没有错误的获取&错误
由于某种原因,当我执行 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];
}
它显示的警报中,[错误代码]
显示 0
,[error localizedDescription]
显示 Null
。
有什么想法吗?
For some reason I am getting an error when no error exists when I do a 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];
}
It is showing the alert with [error code]
showing 0
and the [error localizedDescription]
showing Null
.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要检查
NSError
对象来确定是否发生错误。按照惯例,您应该检查生成错误的方法的返回值。如果结果为nil
或NO
,那么您才应该继续检查错误。此外,您不是在检查错误对象,而是在检查错误对象内存中地址的有效性。在您的情况下,仅当
urlData
为nil
时才继续检查错误。Don't check
NSError
objects to determine if an error has occurred. By convention, you should be checking the return value of the method that generated the error. If the result isnil
orNO
, only then should you proceed with checking the error. Additionally, you're not checking the error object, you're checking for the validity of an address in memory of the error object.In your case, only proceed with inspecting the error if
urlData
isnil
.问题是您正在检查错误对象的地址。您需要检查对象本身。
而不是 后
一种形式计算错误对象的地址,该地址将不为零,因此您的错误条件将始终运行。
(并且不要忘记事先将错误值设置为 nil)。
The problem is you're checking the address of the error object. You need to check the object itself.
instead of
The latter form evaluates the address of the error object, which will be nonzero, so your error condition will always run.
(And don't forget to set the error value to nil beforehand).
首先将错误初始化为 nil:
当您想查看错误时,请执行以下操作:
initialize error to nil first:
and when you want to look at the error, do: