Search code examples
iosobjective-ccore-datansstringnsnumber

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?


Solution

  • You can use stringWithFormat to directly assign value to that NSString variable like this.

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