我正在使用 iOS SDK 5 来开发应用程序,并且正在尝试使用 ARC。但我需要使用 ASIHTTPRequest
并且它不支持 ARC。苹果的文档说使用基于 ARC 文件是可以的。因此,我使用 -fno-objc-arc
编译所有 ASIHTTPRequest
文件。我在我的类中编写了以下代码,该代码使用 ARC:
NSURL *url = [[NSURL alloc] initWithString:urlStr];
ASIHTTPRequest *req = [[ASIHTTPRequest alloc] initWithURL:url];
req.delegate = self;
[req startAsynchronous];
但执行第一行后,url 为 nil。出了什么问题或者如何在 ARC 项目中使用手动管理代码?谢谢。
最佳答案
您是否转义了 URL 字符串中的所有非 ASCII 字符?如果不是,则不会创建 NSURL 实例,因为它不知道如何处理 URL 字符串中的非 ASCII 字符。你需要做这样的事情:
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
另外,为什么不使用 Apple 的辅助方法 URLWithString:
而不是创建自己的 NSURL 实例,它会为您提供一个“自动释放”的 NSURL 实例,如下所示:
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIHTTPRequest *req = [[ASIHTTPRequest alloc] initWithURL:url];
req.delegate = self;
[req startAsynchronous];
关于iphone - 带有 ASIHTTPRequest 的 ARC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7470308/