iphone - 带有 ASIHTTPRequest 的 ARC

标签 iphone ios automatic-ref-counting

我正在使用 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/

相关文章:

ios - NSMutable 字符串问题

iphone - UINavigationController 在 iPhone 中的 ARC 下无法工作

iphone - 我可以构建一个 ARC 框架并将其用于非 ARC 项目吗?

iphone - 向 UIView 添加组合框或下拉列表

iphone - 将 UIButton 数组添加到 navigationItem.rightBarButtonItem 会导致 NSInvalidArgumentException

iphone - iOS 处理多个异步请求 : Send a Signal When All Requests Are Finished

objective-c - 处理ARC中的指针对指针所有权问题

iphone - 推送通知不调用该方法

ios - 如何确定当前的 iPhone/设备型号?

ios - WKWebView 在加载另一个 URL 时不会重新呈现内容