iPhone/iOS isEqualToString 总是返回 false

发布于 2024-11-01 19:42:50 字数 1213 浏览 4 评论 0原文

我的应用程序中有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羅雙樹 2024-11-08 19:42:51

您在调试器中看到的 (null) 是调试器告诉您该值实际上是 nil 的方式。所以你可能想尝试这个:

if ( ! [error localizedFailureReason] ) {
    ...
} ...

The (null) you see in the debugger is the debugger's way of telling you the value is in fact nil. So you might want to try this instead:

if ( ! [error localizedFailureReason] ) {
    ...
} ...
甜嗑 2024-11-08 19:42:50

该字符串不等于“(null)”,该字符串为 nil,当打印到控制台时,该字符串被打印为 AS (null)。

将 nil 传递给 isStringEqual 将始终返回 false。

如果您需要检查 nil,请将字符串与 nil 进行比较,而不是与“(null)”进行比较。

if (error != nil)
{
    errorMessage = [errorMessage stringByAppendingFormat:@"\nReason: %@", [error localizedFailureReason]];
}

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)".

if (error != nil)
{
    errorMessage = [errorMessage stringByAppendingFormat:@"\nReason: %@", [error localizedFailureReason]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文