ios - 我可以将 retain 与 ARC 一起使用吗?

标签 ios objective-c cocoa memory-management automatic-ref-counting

我正在阅读这份官方指南:https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW13我不确定它是否指的是处理引用计数的旧方法,或者只是为了演示它是如何工作的——但是应该像使用访问器的例子那样手动使用保留/释放吗?

最佳答案

您不能将 retain 与 ARC 一起使用。但是,它在没有 ARC 的情况下仍然在后台工作(并且如您链接的文档中所述),但是编译器会根据需要添加 retainrelease 调用.你不必处理那个。

这个二传手:

- (void)setCount:(NSNumber *)newCount {
    [newCount retain];
    [_count release];
    // Make the new assignment.
    _count = newCount;
}

在使用 ARC 时应该是这样的:

- (void)setCount:(NSNumber *)newCount {
    // Make the new assignment.
    _count = newCount;
}

关于ios - 我可以将 retain 与 ARC 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22731533/

相关文章:

macos - 如何在拖动过程中更改拖放图像?

cocoa - 如何使用 cocoa 编程语言运行 .command 文件?

iOS 应用程序在保留在后台时返回到 rootViewController

ios - UITableViewCell 子类不允许我设置标签文本

ios - 使用 SwiftyJSON 处理 JSON 响应

ios - Polidea 的 iOS Class Guard 成功,但无法区分差异

objective-c - 何时为自定义 View 创建 Interface Builder 插件?

ios - 如何展开和折叠 UILabel?

ios - 在 Xcode 6 beta 4 (iOS 8) 中,当我声明它可转换时,为什么 Core Data 不像在 iOS 7 中那样将 UIImage 字段持久化到后端?

cocoa - 优化核心数据获取请求