2

我正在使用 iOS SDK 5 开发应用程序,并且正在尝试使用 ARC。但我需要使用ASIHTTPRequest它并且它没有启用 ARC。Apple 的文档说使用 ARC 文件是可以的。所以我ASIHTTPRequest-fno-objc-arc. 我在使用 ARC 的课堂上编写了以下代码:

NSURL *url = [[NSURL alloc] initWithString:urlStr];
ASIHTTPRequest *req = [[ASIHTTPRequest alloc] initWithURL:url];
req.delegate = self;
[req startAsynchronous];

但执行第一行后,url 为 nil。有什么问题或如何在 ARC 项目中使用手动管理的代码?谢谢。

4

1 回答 1

4

您是否已经转义了 URL 字符串中的所有非 ASCII 字符?如果没有,则不会创建 NSURL 实例,因为它不知道如何处理 URL 字符串中的非 ASCII 字符。你需要做这样的事情:

NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

此外,与其创建您自己的 NSURL 实例,不如使用 Apple 的辅助方法URLWithString:,它会为您提供一个“自动发布”的 NSURL 实例,如下所示:

NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIHTTPRequest *req = [[ASIHTTPRequest alloc] initWithURL:url];
req.delegate = self;
[req startAsynchronous];
于 2011-09-19T12:14:27.650 回答