安全转换,但仍然是 EXC_BAD_ACCESS
免责声明:我对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
statusCode
返回一个NSInteger
(long
或int
),而不是指向NSObject< 的指针/代码> 实例。
格式说明符
%@
用于NSObject
参数。当从statusCode
返回的整数值被解释/传递为指向对象的指针,然后由运行时发送消息或以其他方式处理为指向对象的指针时,可能会出现此问题。当通过%@
打印对象参数时,记录器使用对象的-[NSObject description]
的结果。您可以通过打开编译器警告并纠正它生成的问题来避免将来出现此问题。
statusCode
returns anNSInteger
(along
or anint
), not a pointer to anNSObject
instance.The format specifier
%@
is used forNSObject
s arguments. The problem likely occurs when the integer value that is returned fromstatusCode
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.