Comparing strings in IF statements: unrecognised selector sent to instance
in my code, I need to compare two strings to see if they are equal. if they are it needs to preform a function. one of the strings is just a @"someString", the other is part of an object.
if ([[[mine metal] stringValue] isEqualToString:@"Gold"])
{
//some function
}
however there are some complications when I do this. first, it gives me a warning: NSString may not respond to -stringValue. and when I run the Application it quits out at the if statement: the console reports " -[NSCFString stringValue] : unrecogn开发者_Python百科ized selector sent to instance." mine.metal is defined through a fast enumeration loop across an array; the metal attribute is defined as an NSString, and NSLog is able to display this string. what else am I missing?
The compiler warning and the subsequent run-time error both tell you what the problem is.
[mine metal]
returns an NSString. NSString doesn't have a method called stringValue
.
If [mine metal] does indeed return an NSString then you can do this:
if ([[mine metal] isEqualToString:@"Gold"])
{
//some function
}
精彩评论