我的应用程序中有一个简单的 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 方法将标题和消息输出到控制台,这是第二行的来源。
我已经研究了一段时间了,答案一定很简单,以至于没有引起注意。有什么建议么? (我尝试删除 [error localizedDescription] 中的空格,但无济于事。)我使用的是最新的 4.3 iOS SDK。
最佳答案
字符串不等于“(null)”,字符串为 nil,当打印到控制台时打印为 AS (null)。
将 nil 传递给 isStringEqual
将始终返回 false。
如果您需要检查 nil,请将您的字符串与 nil 进行比较,而不是“(null)”。
if (error != nil)
{
errorMessage = [errorMessage stringByAppendingFormat:@"\nReason: %@", [error localizedFailureReason]];
}
关于ios - iPhone/iOS isEqualToString 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5680947/