2

我有一个字符串dummyString需要与 100 进行比较。这是我的代码

  dummyString = [prefs objectForKey:@"displaySize"];
[self setDisplaySize:dummyString];
NSLog(@"Getting ready to set display controller with value %@", dummyString);


NSLog(@"Tried 1");
if ([dummyString isEqual: @"5"]) {
    NSLog(@"Loaded preferences, display Small");
    displayChanged.selectedSegment = 0;}

NSLog(@"Tried 2");
if ([dummyString isEqual: @"40"]){
    NSLog(@"Loaded preferences, display is Med!");
        displayChanged.selectedSegment = 1;}

NSLog(@"Tried 3");
if ([dummyString isEqualToString:@"100"]){
    NSLog(@"Loaded preferences, display LARGE");
    displayChanged.selectedSegment = 2;}

NSLog(@"Completed trying to get display controller with value %@", dummyString);

这是 NSLog

2013-03-26 17:22:49.101 CopyBot[1011:303] Getting ready to set display controller with value 100
 2013-03-26 17:22:49.102 CopyBot[1011:303] Tried 1
 2013-03-26 17:22:49.102 CopyBot[1011:303] Tried 2
 2013-03-26 17:22:49.102 CopyBot[1011:303] Tried 3
 2013-03-26 17:22:49.103 CopyBot[1011:303] -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x64c7
 2013-03-26 17:22:49.103 CopyBot[1011:303] -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x64c7

我尝试了几种声明不同字符串的变体,但似乎永远无法让它进入'if'语句以进行isEqual或isEqualToString的任何比较。任何帮助表示赞赏。

4

2 回答 2

3

正如您在错误消息中看到的那样[__NSCFNumber isEqualToString:]: unrecognized selector...,问题在于__NSCFNumber没有isEqualToString方法。

__NSCFNumber是 CocoaNSNumber在运行时代表大多数 s 的方式。

所以你的问题是 dummyString 根本不是一个字符串,它是一个NSNumber

于 2013-03-27T00:30:20.863 回答
3

根据您的错误,您确实dummyString不是。NSStringNSNumber

您可以将您的字符串转换@"5"NSNumber然后比较:

BOOL equal=[dummyString isEqualToNumber:@([@"5" integerValue])];

或者。将 dummystring 值转换为字符串,然后进行比较:

BOOL equal=[NSString stringWithFormat:@"%d",[dummyString integerValue]] isEqualToString:@"5"];
于 2013-03-27T00:39:09.243 回答