Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[NSCFString stringValue]: unrecognized selector sent to instance

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 ?

like image 732
Finger twist Avatar asked Sep 29 '10 15:09

Finger twist


4 Answers

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

NSString *value = [mo valueForKey:@"shotNumber"];
like image 119
kennytm Avatar answered Nov 11 '22 18:11

kennytm


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.

like image 29
johnnyb Avatar answered Nov 11 '22 18:11

johnnyb


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

like image 4
bbum Avatar answered Nov 11 '22 18:11

bbum


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.

like image 2
Richard J. Ross III Avatar answered Nov 11 '22 18:11

Richard J. Ross III