gpt4 book ai didi

objective-c - NSString isEqualToString 不会返回 true,并且无法识别选择器

转载 作者:行者123 更新时间:2023-12-03 16:25:51 25 4
gpt4 key购买 nike

我有一个字符串 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])];

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

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

关于objective-c - NSString isEqualToString 不会返回 true,并且无法识别选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15649919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com