Question

I have the error -[NSCFString stringValue]: unrecognized selector sent to instance 0x1578c when executing this code I don't understand what I'm doing wrong name is a NSString

self.searchValues= [[NSMutableArray alloc] init];
name=@"Bob";
if(self.name!=nil)
   [searchValues addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Name",@"Label",self.name,@"Value",nil]];
NSLog(@"Array value : %s",[[[searchValues objectAtIndex:0] objectForKey:@"Value"] stringValue]);
Was it helpful?

Solution

Firstly, you can drop the [... stringValue] message. It isn't necessary, that object is already a string. Secondly, you should use %@ instead of %s for NSString objects.

Note: %@ works for any object at all, for that matter. Try

NSLog(@"Array: %@", searchValues);

Trust me, it's worth trying.

OTHER TIPS

There is no "stringValue" method on NSString, thus the error.

If you really want the value as a C-string, the method you want is UTF8String. For your logging case though, you should just change %s to %@, and log the NSString object directly.

There is no need to use stringValue in this case (edit: it is actually an error, as others have posted, because NSString does not have a stringValue method).

The object that comes out of your dictionary is already an NSString. What you should do is allow the NSLog function to handle the object as a proper cocoa object. The code for that is %@, not %s, since %s is for C-style strings.

Here is how it should look:

self.searchValues = [[NSMutableArray alloc] init];
name=@"Bob";
if(self.name!=nil)
{
    [searchValues addObject: [NSMutableDictionary
           dictionaryWithObjectsAndKeys:@"Name", @"Label",
                                        self.name,@"Value",
                                       nil]];

    NSLog(@"Array value: %@", [[searchValues objectAtIndex:0]
                                 objectForKey:@"Value"]);
}

If you have property for name variable then use self.name when performing any operation on it otherwise there is no meaning of declaring property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top