我有一个字符串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的任何比较。任何帮助表示赞赏。

最佳答案

根据您的错误,您的dummyString不是NSString确实是NSNumber

您可以将字符串@"5"转换为NSNumber,然后进行比较:

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


要么。将dummystring值转换为string,然后进行比较:

BOOL equal=[NSString stringWithFormat:@"%d",[dummyString integerValue]] isEqualToString:@"5"];

07-28 06:20