I'm using this code to query core data and return the value of key, I store the value like this :
NSString *newName= @"test";
[newShot setValue:newName forKey:@"shotNumber"];
and I query like this :
NSManagedObject *mo = [items objectAtIndex:0]; // assuming that array is not empty
NSString *value = [[mo valueForKey:@"shotNumber"] stringValue];
NSLog(@"Value : %@",value);
I'm crashing with this message though :
[NSCFString stringValue]: unrecognized selector sent to instance,
does anyone know where that would be coming from ?
newName
(@"test"
) is already an NSString. There is no need to call -stringValue
to convert it to a string.
NSString *value = [mo valueForKey:@"shotNumber"];
I often times add a category for NSString to handle this:
@interface NSString(JB)
-(NSString *) stringValue;
@end
@implementation NSString(JB)
-(NSString *) stringValue {
return self;
}
@end
You can add a similar category to other classes that you want to respond this way.
[mo valueForKey: @"shotNumber"]
is returning a string and NSString
(of which NSCFString
is an implementation detail) do not implement a stringValue
method.
Given that NSNumber
does implement stringValue
, I'd bet you put an NSString
into mo
when you thought you were putting in an NSNumber
.
The value for the key @"shotNumber"
is probably of type NSString
which is just a wrapper for NSCFString
. What you need to do, is, instead of stringValue
, use the description
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With