views:

74

answers:

3

Hello,

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 ?

A: 

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.

Richard J. Ross III
This is wrong. Never *ever* use `description` in production code. `description` is purely for describing an object for debugging purposes.
bbum
If you have an NSString, you don't need to call any methods to get an NSString from it.
Chuck
Correct, but if his encoding methods are inconsistent, then you should use some method to ensure that it becomes a NSString, maybe `[NSString stringWithFormat:@"%@", [mo valueForKey:@"shotNumber"]]`?
Richard J. Ross III
+2  A: 

[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.

bbum
+3  A: 

newName (@"test") is already an NSString. There is no need to call -stringValue to convert it to a string.

NSString *value = [mo valueForKey:@"shotNumber"];
KennyTM