安全转换,但仍然是 EXC_BAD_ACCESS

发布于 2024-12-27 15:04:38 字数 732 浏览 4 评论 0原文

免责声明:我对 Obj-C 和 iOS(5,启用 ARC)还很陌生。

NSURLConnectionDelegate 方法的以下实现在 if 内的 NSLog 调用中创建 EXC_BAD_ACCESS:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSLog(@"Response %@", response );
    if([response isKindOfClass:[NSHTTPURLResponse class]])
    {
        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*) response;
        NSLog(@"HTTP status code %@", [httpResponse statusCode]);
    }
}

据我设法发现,EXC_BAD_ACCESS 主要是由于分配问题、错误的转换和错误的内存管理引起的。这些都不适用于这里(我希望)。

提前致谢, Chris

解决方案:格式化 og 字符串时出现新手错误。将第二个 NSLog 更改为:

NSLog(@"HTTP status code %i", [httpResponse statusCode]);

Disclaimer: I'm quite new to Obj-C and iOS (5, ARC enabled).

The following implementation of an NSURLConnectionDelegate method creates EXC_BAD_ACCESS in the NSLog call inside the if:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSLog(@"Response %@", response );
    if([response isKindOfClass:[NSHTTPURLResponse class]])
    {
        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*) response;
        NSLog(@"HTTP status code %@", [httpResponse statusCode]);
    }
}

As far as I managed to find out, the EXC_BAD_ACCESS is caused mostly due to allocation issues, wrong casting, and bad memory management. None of that applies here (I hope).

Thanks in advance,
Chris

Solution: Noobie error in formatting the og string. Change the second NSLog to:

NSLog(@"HTTP status code %i", [httpResponse statusCode]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

柒七 2025-01-03 15:04:38

statusCode 返回一个 NSIntegerlongint),而不是指向 NSObject< 的指针/代码> 实例。

格式说明符 %@ 用于 NSObject 参数。当从 statusCode 返回的整数值被解释/传递为指向对象的指针,然后由运行时发送消息或以其他方式处理为指向对象的指针时,可能会出现此问题。当通过 %@ 打印对象参数时,记录器使用对象的 -[NSObject description] 的结果。

您可以通过打开编译器警告并纠正它生成的问题来避免将来出现此问题。

statusCode returns an NSInteger (a long or an int), not a pointer to an NSObject instance.

The format specifier %@ is used for NSObjects arguments. The problem likely occurs when the integer value that is returned from statusCode is interpreted/passed as a pointer to an object and then messaged or otherwise treated as a pointer to an object by the runtime. When an object argument is printed via %@ the logger uses the result of the object's -[NSObject description].

You can avoid this problem in the future by turning up your compiler warnings and correcting the issues it generates.

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