iPhone/iOS isEqualToString 总是返回 false
我的应用程序中有一个简单的 isEqualToString 测试,由于某种原因,它总是采用 if 语句的错误路径,即使控制台显示它应该采用真实路径。
以下是有问题的代码:
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"Program requestFailed with error '%@' and reason '%@'", [error localizedDescription], [error localizedFailureReason]);
NSString *errorMessage = [NSString stringWithFormat:@"%@",[error localizedDescription]];
if ([[error localizedFailureReason] isEqualToString:@"(null)"])
{
}
else
{
errorMessage = [errorMessage stringByAppendingFormat:@"\nReason: %@", [error localizedFailureReason]];
}
[Utils msgBox:@"Error with Data Download" message:errorMessage];
}
在控制台中:
2011-04-15 14:27:07.341 Program[79087:207] Program requestFailed with error 'The request timed out' and reason '(null)'
2011-04-15 14:27:07.341 Program[79087:207] Displaying a message box with title 'Error with Data Download' and message 'The request timed out
Reason: (null)'
我的 Utils 类中的 msgBox 方法将标题和消息输出到控制台,这就是第二行的来源。
我已经研究这个问题有一段时间了,答案一定很简单,以至于被忽视了。有什么建议吗? (我尝试修剪[错误 localizedDescription] 中的空格,但无济于事。)我使用的是最新的 4.3 iOS SDK。
I have a simple isEqualToString test in my application, and for some reason, it is always taking the false path of the if statement, even though the console shows that it should be taking the true path.
Here is the code that is in question:
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"Program requestFailed with error '%@' and reason '%@'", [error localizedDescription], [error localizedFailureReason]);
NSString *errorMessage = [NSString stringWithFormat:@"%@",[error localizedDescription]];
if ([[error localizedFailureReason] isEqualToString:@"(null)"])
{
}
else
{
errorMessage = [errorMessage stringByAppendingFormat:@"\nReason: %@", [error localizedFailureReason]];
}
[Utils msgBox:@"Error with Data Download" message:errorMessage];
}
And in the console:
2011-04-15 14:27:07.341 Program[79087:207] Program requestFailed with error 'The request timed out' and reason '(null)'
2011-04-15 14:27:07.341 Program[79087:207] Displaying a message box with title 'Error with Data Download' and message 'The request timed out
Reason: (null)'
The msgBox method in my Utils class outputs the title and message to the console, which is where the second line comes from.
I have been looking at this for a while, and the answer must be so easy that it escapes notice. Any suggestions? (I tried to trim the white spaces off [error localizedDescription], to no avail.) I am on the latest 4.3 iOS SDK.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在调试器中看到的
(null)
是调试器告诉您该值实际上是nil
的方式。所以你可能想尝试这个:The
(null)
you see in the debugger is the debugger's way of telling you the value is in factnil
. So you might want to try this instead:该字符串不等于“(null)”,该字符串为 nil,当打印到控制台时,该字符串被打印为 AS (null)。
将 nil 传递给
isStringEqual
将始终返回 false。如果您需要检查 nil,请将字符串与 nil 进行比较,而不是与“(null)”进行比较。
The string isn't equal to "(null)" the string is nil, which when printed to the console is printed AS (null).
Passing nil to
isStringEqual
will always return false.If you need to check for nil, compare your string to nil, not "(null)".