开发者

Comparing strings in IF statements: unrecognised selector sent to instance

开发者 https://www.devze.com 2022-12-17 16:36 出处:网络
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.

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号