没有错误的获取&错误

发布于 2024-12-20 07:40:21 字数 999 浏览 8 评论 0原文

由于某种原因,当我执行 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 技术交流群。

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

发布评论

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

评论(3

少年亿悲伤 2024-12-27 07:40:21

不要检查 NSError 对象来确定是否发生错误。按照惯例,您应该检查生成错误的方法的返回值。如果结果为 nilNO,那么您才应该继续检查错误。此外,您不是在检查错误对象,而是在检查错误对象内存中地址的有效性。

在您的情况下,仅当 urlDatanil 时才继续检查错误。

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 is nil or NO, 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 is nil.

海之角 2024-12-27 07:40:21

问题是您正在检查错误对象的地址。您需要检查对象本身。

if (error != nil)

而不是 后

if (&error)

一种形式计算错误对象的地址,该地址将不为零,因此您的错误条件将始终运行。

(并且不要忘记事先将错误值设置为 nil)。

The problem is you're checking the address of the error object. You need to check the object itself.

if (error != nil)

instead of

if (&error)

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

望笑 2024-12-27 07:40:21

首先将错误初始化为 nil:

NSError * error = nil;

当您想查看错误时,请执行以下操作:

if(error)
{
    //Handle Error

initialize error to nil first:

NSError * error = nil;

and when you want to look at the error, do:

if(error)
{
    //Handle Error
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文