出于某种原因,当我执行 NSURLConnection
时没有错误存在,但我收到错误。
NSLog(@"Sending string to server. ID:11118");
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response from server received. ID:11119");
NSString *responseString = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
if (&error)
{
//Handle Error
NSLog(@"Error getting a server response! (scm) Error %i: %@", [error code], [error localizedDescription]);
UIAlertView *theAlert = [[UIAlertView alloc]initWithTitle:@"Error getting a server response!" message:[NSString stringWithFormat:@"Error %i: %@", [error code], [error localizedDescription]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[theAlert show];
}
[error code]
显示 0
和 [error localizedDescription]
显示 Null
>.
有什么想法吗?
最佳答案
不要检查 NSError
对象来确定是否发生了错误。按照惯例,您应该检查产生错误的方法的返回值。如果结果为 nil
或 NO
,那么您才应该继续检查错误。此外,您不是在检查错误对象,而是在检查错误对象内存中地址的有效性。
在您的情况下,仅当 urlData
为 nil
时才继续检查错误。
关于objective-c - 获取 &error 没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8438253/