ios - iPhone/iOS isEqualToString 总是返回 false

标签 ios ios4 nsstring

我的应用程序中有一个简单的 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/

相关文章:

iPhone - 从 UIWindow 启动 UIImagePickerController

ios - 遍历数组时遇到问题

iphone - 在 Facebook 墙上添加照片

objective-c - 从 NSString 中提取括号之间的值

ios - 我不想快速更新 admob 横幅广告

ios - 在 UICollectionView 中显示错误的图像

iphone - 导出 @2x 图像以进行视网膜显示的简单方法?

iphone - TabBarController setSelectedIndex - 将调用哪个委托(delegate)方法

objective-c - 将 NSData objectiveC 代码转换为 swift 时出错

ios - Swift:对泛型类型 Range 的引用需要参数