ARC什么时候工作?编译还是运行时?
When ARC working? Compilation or runtime?
在Objective-C或Swift时ARC(自动引用计数)工作?在编译还是 运行 时间阶段?为什么重要?
编译器在编译时插入必要的 retain
/release
调用,但这些调用在运行时执行,就像任何其他代码一样。
来自 Apple 的 Transitioning to ARC Release Notes
ARC is a compiler feature that provides automatic memory management of Objective-C objects.
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.
ARC 启用示例 class:
@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@end
@implementation Person
// ARC insert dealloc method & associated memory
// management calls at compile time.
- (void)dealloc
{
[super dealloc];
[_firstName release];
[_lastName release];
}
@end
在Objective-C或Swift时ARC(自动引用计数)工作?在编译还是 运行 时间阶段?为什么重要?
编译器在编译时插入必要的 retain
/release
调用,但这些调用在运行时执行,就像任何其他代码一样。
来自 Apple 的 Transitioning to ARC Release Notes
ARC is a compiler feature that provides automatic memory management of Objective-C objects.
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.
ARC 启用示例 class:
@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@end
@implementation Person
// ARC insert dealloc method & associated memory
// management calls at compile time.
- (void)dealloc
{
[super dealloc];
[_firstName release];
[_lastName release];
}
@end