【问题标题】:autorelease deprecated, is there an alternative?不推荐使用自动释放,还有其他选择吗?
【发布时间】:2013-10-02 20:31:16
【问题描述】:

按照代码的照片幻灯片教程,我遇到了一些我认为已弃用的代码。我在autorelease 有一个ARC 错误,在setDelegate:self 有一个警告

NSXMLParser *photoParser = [[[NSXMLParser alloc]
                                 initWithContentsOfURL:[NSURL URLWithString:
                                                        @"http://localhost/photos/index.xml"]] autorelease];
    [photoParser setDelegate:self];

【问题讨论】:

  • 它没有被“弃用”(意味着将来不会支持它)。使用 ARC 时不需要编写它(编译器会在适合您的地方输入retainreleaseautorelease 等)。

标签: objective-c automatic-ref-counting autorelease


【解决方案1】:

删除自动释放,使代码如下所示:

ARC 不允许显式自动释放调用。如果你想释放一个对象,你不需要做任何事情(ARC 会处理它)。在某些情况下,您可能希望将对象设置为 nil(例如 photoParser = nil;)

NSXMLParser *photoParser = [[NSXMLParser alloc]
                                 initWithContentsOfURL:[NSURL URLWithString:
                                                        @"http://localhost/photos/index.xml"]];
[photoParser setDelegate:self];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2011-04-21
    • 1970-01-01
    • 2015-08-30
    相关资源
    最近更新 更多