开发者

iPhone/iOS isEqualToString always returning false

开发者 https://www.devze.com 2023-02-26 18:37 出处:网络
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 pat

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.


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]];
}


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] ) {
    ...
} ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号