Swemoph Swemoph - 3 months ago 21
Objective-C Question

NSNumber * to NSString * from CoreData

I currently have an

NSArray
of objects that have come from CoreData - one of the properties (named
identifier
) is an
NSNumber
. I'm doing some comparisons in my code and need to convert this
NSNumber
to a
NSString
.

I've got this to work, however I am wondering what the difference between these two pieces of code is:

NSNumber * arrayValueNum = [coreArray[i] identifier];
NSString * arrayValue = [arrayValueNum stringValue];


and

NSString * test = [[coreArray[i] identifier] stringValue];


For some reason this second snippet will not compile, and the first one will.

Error:


no visible @interface for 'NSString' declares the selector 'stringValue'


Aren't they the same, except that the
NSNumber *
value is in the braces instead of being in it's own variable?

Answer

Compiler is saying that [coreArray[i] identifier] is already NSString and NSString doesn't have method stringValue, so try to write like this.

NSString *test = [NSString stringWithFormat:@"%@",[coreArray[i] identifier]];