Transitioning to ARC Release Notes

[翻译自官方文档]

Automatic Reference Counting (ARC) is a compiler feature that provides automatic memory management of Objective-C objects. Rather than having to think about retain and release operations, ARC allows you to concentrate on the interesting code, the object graphs, and the relationships between objects in your application.

自动引用计数(ARC)是一个编译特征,它提供对Objective-C对象进行自动内存管理。不需要思考保留(retain)和释放(release) 操作,ARC让你把精力集中在代码上,对象图谱上,以及对象之间的关系。

Summary

概要

ARC works by adding code at compile time to ensure that objects live as long as necessary, but no longer. Conceptually, it follows the same memory management conventions as manual reference counting (described in Advanced Memory Management Programming Guide) by adding the appropriate memory management calls for you.

ARC通过编译时插入代码确保对象需要时保留,但不需要时就释放。它为你增加合适的内存管理调用,从这点看它和手动引用计数(  参考   Advanced Memory Management Programming Guide) 遵循相同的内存管理规则。

In order for the compiler to generate correct code, ARC restricts the methods you can use and how you use toll-free bridging (see “Toll-Free Bridged Types”). ARC also introduces new lifetime qualifiers for object references and declared properties.

为了能让编译器生成正确的代码,ARC研究限制你使用的方法,以及你如何使用 toll-free bridging (参考 “Toll-Free Bridged Types”).ARC还为对象引用和声明属性引入了新的生命周期限定语。

ARC is supported in Xcode 4.2 for OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5. Weak references are not supported in OS X v10.6 and iOS 4.

ARC 支持Xcode4.2 以及iOS4,iOS5。 弱引用不支持10.6和iOS4。

Xcode provides a tool that automates the mechanical parts of the ARC conversion (such as removing retain and release calls) and helps you to fix issues the migrator can’t handle automatically (choose Edit > Refactor > Convert to Objective-C ARC). The migration tool converts all files in a project to use ARC. You can also choose to use ARC on a per-file basis if it’s more convenient for you to use manual reference counting for some files.

Xcode 提供了一个自动装换ARC的工作(比如出去保留和释放调用),并且帮助你解决迁移器无法自动解决的问题。(choose Edit > Refactor > Convert to Objective-C ARC). 迁移工具转变工程中的所有的文件去使用ARC。如果你觉得对于某些文件使用手动引用计数比较好,那你可以选择针对某些文件使用ARC。

See also:

ARC Overview

Instead of you having to remember when to use retain, release, and autorelease, ARC evaluates the lifetime requirements of your objects and automatically inserts appropriate memory management calls for you at compile time. The compiler also generates appropriate dealloc methods for you.

 In general, if you’re only using ARC the traditional Cocoa naming conventions are important only if you need to interoperate with code that uses manual reference counting.

A complete and correct implementation of a Person class might look like this:

@interface Person : NSObject


@property NSString *firstName;


@property NSString *lastName;


@property NSNumber *yearOfBirth;


@property Person *spouse;


@end


 


@implementation Person


@end


(Object properties are strong by default; the strong attribute is described in “ARC Introduces New Lifetime Qualifiers.”)

Using ARC, you could implement a contrived method like this:

- (void)contrived {


    Person *aPerson = [[Person alloc] init];


    [aPerson setFirstName:@"William"];


    [aPerson setLastName:@"Dudney"];


    [aPerson setYearOfBirth:[[NSNumber alloc] initWithInteger:2011]];


    NSLog(@"aPerson: %@", aPerson);


}


ARC takes care of memory management so that neither the Person nor the NSNumber objects are leaked.

You could also safely implement a takeLastNameFrom: method of Person like this:

- (void)takeLastNameFrom:(Person *)person {


    NSString *oldLastname = [self lastName];


    [self setLastName:[person lastName]];


    NSLog(@"Lastname changed from %@ to %@", oldLastname, [self lastName]);


}


ARC ensures that oldLastName is not deallocated before the NSLog statement.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值