转向ARC的说明
轉(zhuǎn)自hherima的博客
原文:Transitioning to ARC Release Notes(蘋果官方文檔)
?
ARC是一個(gè)編譯器特征,它提供了對(duì)OC對(duì)象自動(dòng)管理內(nèi)存。ARC讓開發(fā)者專注于感興趣的代碼和對(duì)象的關(guān)系,而不用考慮對(duì)象的retain和release。
?
概要
ARC在編譯時(shí)期添加代碼,保證對(duì)象可用。概念上說,ARC遵循手動(dòng)引用計(jì)數(shù)的規(guī)則,替開發(fā)者,在編譯時(shí)期添加合適的代碼。
?
Xcode4.2(Mac OS 10.6、10.7和iOS4和iOS5)支持ARC,弱引用在10.6和iOS4上不支持。
?
Xcode提供了一個(gè)工具:自動(dòng)機(jī)械得轉(zhuǎn)化為ARC(比如移除retain和release的調(diào)用),并幫助開發(fā)者解決不能自動(dòng)遷移的問題。遷移工具將所有文件轉(zhuǎn)化成ARC,開發(fā)者也可以對(duì)單個(gè)文件實(shí)施ARC,方便于開發(fā)者對(duì)某些文件手動(dòng)引用計(jì)數(shù)。
?
ARC概要
ARC評(píng)估了對(duì)象的生命周期,并自動(dòng)插入合適的代碼在編譯時(shí)期,從而代替開發(fā)者不得不考慮何時(shí)需要retain、release和autolease。同編譯器替開發(fā)者,生成合適的dealloc方法。一般來說,當(dāng)開發(fā)者需要在手動(dòng)引用計(jì)數(shù)和ARC配合使用時(shí)候,使用傳統(tǒng)ARC的Cocoa命名慣例是很重要的。
?
一個(gè)正確的完整的Person類如下:
?
@interface Person : NSObject? ?
@property NSString *firstName;? ?
@property NSString *lastName;? ?
@property NSNumber *yearOfBirth;? ?
@property Person *spouse;? ?
@end? ?
?? ?
@implementation Person? ?
@end? ?
?
對(duì)象的屬性默認(rèn)是strong;strong屬性在 “ARC Introduces New Lifetime Qualifiers.”有描述
?
- (void)contrived {? ?
??? Person *aPerson = [[Person alloc] init];? ?
??? [aPerson setFirstName:@"William"];? ?
??? [aPerson setLastName:@"Dudney"];? ?
??? [aPerson setYearOfBirth:[[NSNumber alloc] initWithInteger:2011]];? ?
??? NSLog(@"aPerson: %@", aPerson);? ?
}? ?
?
ARC掌管了內(nèi)存管理,所以Person和NSNumber不會(huì)泄露。
?
開發(fā)者還可以安全得實(shí)現(xiàn)Person類的方法takeLastNameFrom:
?
- (void)takeLastNameFrom:(Person *)person {? ?
??? NSString *oldLastname = [self lastName];? ?
??? [self setLastName:[person lastName]];? ?
??? NSLog(@"Lastname changed from %@ to %@", oldLastname, [self lastName]);? ?
}? ?
?
ARC確保oldLastName在NSLog之前不會(huì)被銷毀。
?
ARC執(zhí)行了新的規(guī)則
ARC執(zhí)行了一些新的規(guī)則,是其他編譯模式?jīng)]有的。這個(gè)規(guī)則是:致力于提供一個(gè)安全可靠的內(nèi)存管理模式。在某些情況下,他們只是執(zhí)行最佳實(shí)踐;某些情況,他們簡(jiǎn)化你的代碼或者處理那沒明顯的不需要內(nèi)存問題。如果你違反這些規(guī)則,立即得到一個(gè)編譯錯(cuò)誤,而不會(huì)在運(yùn)行顯示一個(gè)微妙的bug。
?
● 開發(fā)者不能顯示調(diào)用dealloc;不能實(shí)現(xiàn)和調(diào)用retain、release、retainCount和autorelease。
禁止使用@selector(retain),@selector(release)等等。
開發(fā)者仍可以實(shí)現(xiàn)dealloc方法,如果你想管理資源而不是變量。開發(fā)者不能release變量,但是可以調(diào)用[systemClassInstance setDelegate:nil]在系統(tǒng)類上,或者其他不是用ARC編譯的代碼上。
ARC中自定義的dealloc方法,不需要調(diào)用[super dealloc](其實(shí)這樣做就會(huì)導(dǎo)致編譯錯(cuò)誤),編譯器會(huì)強(qiáng)制自動(dòng)鏈接到父類。
開發(fā)者仍可以對(duì)Core Foundation-style對(duì)象,使用CFRetain,CFRelease和其他相關(guān)方法。
?
● 不能使用NSAllocateObject或者NSDeallocateObject。開發(fā)者創(chuàng)建對(duì)象使用alloc,運(yùn)行時(shí)環(huán)境負(fù)責(zé)銷毀對(duì)象。
?
● 在C數(shù)據(jù)結(jié)構(gòu)中,不能使用對(duì)象指針。可以使用OC類來代替C的struct
?
● id和void*沒有自動(dòng)轉(zhuǎn)換.
開發(fā)者必須使用特殊的類型轉(zhuǎn)化.開發(fā)者需要通過函數(shù)參數(shù)傳遞,在OC對(duì)象和Core Foundation之間做類型轉(zhuǎn)換。
?
● 開發(fā)者不能使用NSAutoreleasePool對(duì)象。ARC下使用@autoreleasepool,它比NSAtuoreleasePool更有效率。
?
● 開發(fā)者不能使用內(nèi)存zones。不用再使用NSZone了。他們已經(jīng)被現(xiàn)代的OC運(yùn)行環(huán)境給忽略了。
?
為了配合手動(dòng)引用計(jì)數(shù),ARC的方法命名有限制:
● 訪問器方法不能已new開頭,反過來就是:開發(fā)者不能聲明一個(gè)已new開頭的屬性,除非你給你指定一個(gè)getter
?
// 不正確:? ?
@property NSString *newTitle;? ?
?? ?
// 正確:? ?
@property (getter=theNewTitle) NSString *newTitle;? ?
?
ARC引入了新的Lifetime修飾符。
?
ARC引入幾個(gè)新的修飾符和弱(weak)引用。弱引用不會(huì)延伸到對(duì)象生命周期,并自動(dòng)設(shè)置為nil,當(dāng)該對(duì)象沒有任何的強(qiáng)引用的時(shí)候。
?
開發(fā)者需要使用這些修飾來管理對(duì)象圖。通常ARC不對(duì)循環(huán)引用做警告。謹(jǐn)慎得使用弱引用可以保證不會(huì)循環(huán)引用。
?
Property屬性
weak和strong關(guān)鍵字作為新的property屬性被引入,例如:
?
// The following declaration is a synonym for: @property(retain) MyClass *myObject;? ?
@property(strong) MyClass *myObject;? ?
?? ?
// The following declaration is similar to "@property(assign) MyClass *myObject;"? ?
// except that if the MyClass instance is deallocated,? ?
// the property value is set to nil instead of remaining as a dangling pointer.? ?
@property(weak) MyClass *myObject;? ?
?
ARC下,strong是默認(rèn)property屬性
?
變量的修飾符
開發(fā)者使用下面得lifetime修飾。
__strong
__weak
__unsafe_unretained
__autoreleasing
?
● 默認(rèn)是__strong。只要對(duì)象還有強(qiáng)引用,該對(duì)象“活著”。
● __weak不保留對(duì)象,只是簡(jiǎn)單引用。weak對(duì)象將被設(shè)置nil,當(dāng)對(duì)象沒有任何強(qiáng)引用的時(shí)候。
● __unsafe_unretained 不保留對(duì)象,只是簡(jiǎn)單引用。但是不設(shè)置為nil,當(dāng)對(duì)象沒有任何強(qiáng)引用得時(shí)候。如果對(duì)象被銷毀,__unsafe_unretained的對(duì)象將會(huì)野指針。
● __autoreleasing 用于標(biāo)識(shí)id*的引用參數(shù),或者需要自動(dòng)釋放的返回的對(duì)象。
?
開發(fā)者需要正確修飾變量。使用下面的格式來修飾變量聲明。
?
類名*? 修飾? 變量名
例如:
?
MyClass * __weak myWeakReference;? ?
MyClass * __unsafe_unretained myUnsafeReference;? ?
?
其他的變種在技術(shù)上是不正確的,但可能可以通過編譯。了解更多http://cdecl.org/
?
在棧上小心使用__weak。考慮下面的代碼:
NSString * __weak string = [[NSString alloc] initWithFormat:@"First Name: %@", [self firstName]];? ?
NSLog(@"string: %@", string);? ?
盡管string在初始化后被使用了。但是,由于在賦值的時(shí)候沒有強(qiáng)引用;因此它將立即被銷毀。
?
開發(fā)者同樣需要注意傳遞引用的地方,例如下面的代碼:
NSError *error;? ?
BOOL OK = [myObject performOperationWithError:&error];? ?
if (!OK) {? ?
??? // Report the error.? ?
??? // ...? ?
?
然而,這種錯(cuò)誤是隱含的。
NSError * __strong e;? ?
并且聲明的方法可能是下面這樣:
-(BOOL)performOperationWithError:(NSError * __autoreleasing *)error;? ?
編譯器將重寫代碼:
NSError * __strong error;? ?
NSError * __autoreleasing tmp = error;? ?
BOOL OK = [myObject performOperationWithError:&tmp];? ?
error = tmp;? ?
if (!OK) {? ?
??? // Report the error.? ?
??? // ...? ?
本地變量聲明(__strong)和參數(shù)(__autoreleasing)導(dǎo)致編譯器創(chuàng)建臨時(shí)變量。當(dāng)開發(fā)者對(duì)__strong對(duì)象取地址,將參數(shù)聲明為id __strong*,就得到原始的指針。或者開發(fā)者可以將變量聲明為__autoreleasing
?
使用Lifetime修飾符,避免循環(huán)引用
?
開發(fā)者可以通過lifetime修飾符來避免循環(huán)引用。例如,如果你有一個(gè)對(duì)象圖(關(guān)于父-子繼承)。并且父類需要引用子類,相反子類也要用。這時(shí)候,開發(fā)者把parent-to-child寫做strong并把child-to-parent關(guān)系寫作weak。其他情況可能有些麻煩,特別是調(diào)用block變量的時(shí)候。
?
手動(dòng)引用計(jì)數(shù)模式下,__block id x 這樣的寫法,不會(huì)對(duì)x進(jìn)行retain。在ARC模式,__block id x;默認(rèn)retain x(就像其他變量一樣)。如果想在ARC模式下,得到手動(dòng)引用計(jì)數(shù)的效果,開發(fā)者可以使用__unsafe_unretained __block id x。顧名思義,__unsafe_unretained __block id x是危險(xiǎn)的(因?yàn)樗赡軐?dǎo)致野指針)因此也不建議使用。好的解決方案是:使用__weak或者將__block的值設(shè)置為nil,來打斷retain循環(huán)。
下面的代碼段展示了如何使用手動(dòng)引用計(jì)數(shù)
MyViewController *myController = [[MyViewController alloc] init…];? ?
// ...? ?
myController.completionHandler =? ^(NSInteger result) {? ?
?? [myController dismissViewControllerAnimated:YES completion:nil];? ?
};? ?
[self presentViewController:myController animated:YES completion:^{? ?
?? [myController release];? ?
}];? ?
正如說的那樣,開發(fā)者可以使用__block修飾符并且設(shè)置myController變量為nil,在完成處理的時(shí)候。
MyViewController * __block myController = [[MyViewController alloc] init…];? ?
// ...? ?
myController.completionHandler =? ^(NSInteger result) {? ?
??? [myController dismissViewControllerAnimated:YES completion:nil];? ?
??? myController = nil;? ?
};? ?
或者,你可以使用臨時(shí)的__weak變量。下面是一個(gè)簡(jiǎn)單的實(shí)現(xiàn)
MyViewController *myController = [[MyViewController alloc] init…];? ?
// ...? ?
MyViewController * __weak weakMyViewController = myController;? ?
myController.completionHandler =? ^(NSInteger result) {? ?
??? [weakMyViewController dismissViewControllerAnimated:YES completion:nil];? ?
};? ?
對(duì)于non-trivial循環(huán),開發(fā)者應(yīng)該使用下面代碼:
?
MyViewController *myController = [[MyViewController alloc] init…];? ?
// ...? ?
MyViewController * __weak weakMyController = myController;? ?
myController.completionHandler =? ^(NSInteger result) {? ?
??? MyViewController *strongMyController = weakMyController;? ?
??? if (strongMyController) {? ?
??????? // ...? ?
??????? [strongMyController dismissViewControllerAnimated:YES completion:nil];? ?
??????? // ...? ?
??? }? ?
??? else {? ?
??????? // Probably nothing...? ?
??? }? ?
};? ?
?
在某些情況,開發(fā)者使用__unsafe_unretained,如果類本身不是__weak修飾。然而,這樣將變得不切合實(shí)際了,因?yàn)樗赡芎茈y或者不可能去驗(yàn)證:__unsafe_unretained的指針仍然可用并且指向某些變量。
?
ARC使用一個(gè)新語句管理Autorelease Pools
使用ARC,開發(fā)者不能直接使用NSAutoreleasePool來管理autorelease pools。而使用@autoreleasepool代替它。
@autoreleasepool {? ?
???? // Code, such as a loop that creates a large number of temporary objects.? ?
}? ?
這個(gè)簡(jiǎn)單的結(jié)構(gòu)允許編譯器思考引用計(jì)數(shù)的狀態(tài)。進(jìn)入的時(shí)候,自動(dòng)釋放池被push。在正常退出的時(shí)候自動(dòng)釋放池配poped出來。為了配合現(xiàn)有代碼,如果代碼異常退出,自動(dòng)釋放池將不會(huì)pop出來。它比NSAutoreleasePool更有效率;因此建議開發(fā)者替換NSAtuoreleasePool。
?
Patterns for Managing Outlets Become Consistent Across Platforms(略)
?
棧變量被初始化為nil
?
使用ARC,strong、weak和autoreleasing棧變量將不會(huì)顯示初始化為nil,例如:
- (void)myMethod {? ?
??? NSString *name;? ?
??? NSLog(@"name: %@", name);? ?
}? ?
?
將打印出null,而不是崩潰。
?
使用編譯器選項(xiàng)來開啟和關(guān)閉ARC
?
開發(fā)者使用-fobjc-arc 編譯選項(xiàng)開啟ARC,還可以對(duì)某一個(gè)文件使用ARC,便于在使用手動(dòng)引用計(jì)數(shù)的文件中使用ARC。對(duì)于已經(jīng)使用ARC的工程,仍可以指定一個(gè)文件來關(guān)閉ARC通過-fno-objc-arc編譯選項(xiàng)。
?
管理Toll-Free Bridging
?
在多數(shù)的Cocoa程序中,開發(fā)者需要使用Core Foundaton-style對(duì)象,無論是從Core Foundation框架還是從Core foundation的其他框架比如Core Graphics。
?
編譯器不會(huì)自動(dòng)管理Core foundation對(duì)象的生命周期。開發(fā)者必須根據(jù)COreFoundation的內(nèi)存管理規(guī)則,使用CFRetain和CFRelease。
?
如果開發(fā)者在OC和Core foundation兩種對(duì)象做轉(zhuǎn)換,需要告訴編譯器對(duì)象的所有權(quán)。
?
● __bridge 不改變所有權(quán)的情況下,將OC和Core foundaton對(duì)象之間轉(zhuǎn)換。
?
● __bridge_retained 或者 CFBridgingRetain 或者對(duì)象的所有權(quán),將OC和Corefoundaton對(duì)象之間轉(zhuǎn)換。開發(fā)者仍有責(zé)任將釋放對(duì)象通過CFRelease。
?
● __bridge_transfer 或者CFBridgingRelease將一個(gè)非OC指針,轉(zhuǎn)化為OC指針,ARC負(fù)責(zé)釋放對(duì)象。
?
例如,現(xiàn)有代碼:
- (void)logFirstNameOfPerson:(ABRecordRef)person {? ?
?? ?
??? NSString *name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);? ?
??? NSLog(@"Person's first name: %@", name);? ?
??? [name release];? ?
}? ?
?
開發(fā)用下面的代碼代替
- (void)logFirstNameOfPerson:(ABRecordRef)person {? ?
?? ?
??? NSString *name = (NSString *)CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));? ?
??? NSLog(@"Person's first name: %@", name);? ?
}? ?
?
編譯器處理從COcoa方法返回的 CF 對(duì)象
?
編譯器知道返回Core foundaion類的OC的方法,遵循歷史的規(guī)定。例如,編譯器知道從CGColor方法返回的GCColor是不擁有的。開發(fā)者必須使用合適的類型去轉(zhuǎn)化。例如:
NSMutableArray *colors = [NSMutableArray arrayWithObject:(id)[[UIColor darkGrayColor] CGColor]];? ?
[colors addObject:(id)[[UIColor lightGrayColor] CGColor]];? ?
?
使用所有權(quán)關(guān)鍵字轉(zhuǎn)化函數(shù)參數(shù)
?
當(dāng)開發(fā)者在OC和Core foundation的對(duì)象之間轉(zhuǎn)化時(shí),需要告訴編譯器傳遞object的所有權(quán)。Core foundation對(duì)象所有權(quán)的規(guī)則在CoreFoundation的內(nèi)存管理規(guī)則中。OC的所有權(quán)規(guī)則在Advanced Memory Management Programming Guide中。
?
下面的代碼段,數(shù)組傳遞給CGGradientCreateWithCorlors函數(shù)需要一個(gè)合適的轉(zhuǎn)化。數(shù)組是arrayWitshObjects返回的,所以,不要將所有權(quán)傳遞給函數(shù),因此使用__bridge
NSArray *colors = <#An array of colors#>;? ?
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations); ?
下面代碼段實(shí)現(xiàn)了一個(gè)context,使用Core Foundation內(nèi)存管理的方法。
- (void)drawRect:(CGRect)rect {? ?
??? CGContextRef ctx = UIGraphicsGetCurrentContext();? ?
??? CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();? ?
??? CGFloat locations[2] = {0.0, 1.0};? ?
??? NSMutableArray *colors = [NSMutableArray arrayWithObject:(id)[[UIColor darkGrayColor] CGColor]];? ?
??? [colors addObject:(id)[[UIColor lightGrayColor] CGColor]];? ?
??? CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);? ?
??? CGColorSpaceRelease(colorSpace);? // Release owned Core Foundation object.? ?
??? CGPoint startPoint = CGPointMake(0.0, 0.0);? ?
??? CGPoint endPoint = CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMaxY(self.bounds));? ?
??? CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint,? ?
??????????????????????????????? kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);? ?
??? CGGradientRelease(gradient);? // Release owned Core Foundation object.? ?
}? ?
?
將工程轉(zhuǎn)化為ARC,遇到的常見問題
?
當(dāng)遷移現(xiàn)有的項(xiàng)目,你可能會(huì)遇到的各種問題。這里有一些共同的問題,共同解決方案。
?
● 不能調(diào)用retain,release或者autorelease,這是一種特性,甚至可以這么寫:
?
while ([x retainCount]) { [x release]; }? ?
?
?
● 不能調(diào)用dealloc
通常,開發(fā)者在單例的實(shí)現(xiàn)或者替換一個(gè)對(duì)象的init方法的時(shí)候調(diào)用dealloc。對(duì)于單例模式,使用共享實(shí)例模式。在init方法中,你不用調(diào)用dealloc,因?yàn)閷?duì)象會(huì)被釋放,當(dāng)重寫self的時(shí)候。
?
● 不能使用NSAutoreleasePool對(duì)象
?
表格@autoreleasepool{}代替NSAutoreleasePool.這將強(qiáng)制一個(gè)block處于一個(gè)自動(dòng)釋放池中。它比NSAutoreleaePool快6倍。@autoreleasepool也在在非ARC模式下工作。
?
● ARC需要開發(fā)者對(duì)[super init]賦值給self在init方法中。
?
下面代碼是不可行的
[super init];? ?
簡(jiǎn)單的修正是:
self = [super init];? ?
根號(hào)的修復(fù)是這樣的
?
self = [super init];? ?
if (self) {? ?
?? ...? ?
?
● 不能實(shí)現(xiàn)retain和release方法。
實(shí)現(xiàn)自定義的retain和release方法打破弱指針。提供了幾個(gè)常用的 “實(shí)現(xiàn)自定義”的原因。
?
1.性能
不要這么做,NSObject的retain和release已經(jīng)很快了。如果你發(fā)現(xiàn)仍有問題,請(qǐng)?zhí)岢鲞@個(gè)bug
?
2.實(shí)現(xiàn)一個(gè)自定義的弱指針系統(tǒng)
使用__weak代替
?
3.實(shí)現(xiàn)單例
使用shared instance pattern代替。或者使用類來代替方法,避免分配對(duì)象。
?
● assigned 將變成strong
?
在ARC之前,變量的assigned的不會(huì)延伸到對(duì)象的生命周期。為了讓property變成強(qiáng)引用,開發(fā)者通常實(shí)例化或者synthesized訪問器方法,里面是內(nèi)存管理方法。相比之下,你可以這樣實(shí)現(xiàn)訪問器方法:
@interface MyClass : Superclass {? ?
??? id thing; // Weak reference.? ?
}? ?
// ...? ?
@end? ?
?? ?
@implementation MyClass? ?
- (id)thing {? ?
??? return thing;? ?
}? ?
- (void)setThing:(id)newThing {? ?
??? thing = newThing;? ?
}? ?
// ...? ?
@end? ?
ARC下,實(shí)例中的變量默認(rèn)是strong引用,assigning一個(gè)對(duì)象給實(shí)例中的變量延伸到對(duì)象的生命周期。遷移工具不能決定一個(gè)實(shí)例變量即將weak。保持相同的行為之前,你必須標(biāo)記實(shí)例變量是weak,或聲明它的property
?
@interface MyClass : Superclass {? ?
??? id __weak thing;? ?
}? ?
// ...? ?
@end? ?
?? ?
@implementation MyClass? ?
- (id)thing {? ?
??? return thing;? ?
}? ?
- (void)setThing:(id)newThing {? ?
??? thing = newThing;? ?
}? ?
// ...? ?
@end? ?
?
或者
?
@interface MyClass : Superclass? ?
@property (weak) id thing;? ?
// ...? ?
@end? ?
?? ?
@implementation MyClass? ?
@synthesize thing;? ?
// ...? ?
@end? ?
?
● 不能使用strong ids在c的數(shù)據(jù)結(jié)構(gòu)
例如,下面的c結(jié)構(gòu)將不能編譯過
struct X { id x; float y; };? ?
這是因?yàn)閤默認(rèn)是strong retain,但是,在正常運(yùn)行情況下,編譯器不能安全的合成所有需要的代碼。比如:如果你傳遞一個(gè)指針給x或者y后,執(zhí)行了free,每一個(gè)id將被release掉,在struct被free之前。編譯器不能可靠的做這些。所以strong ids在結(jié)構(gòu),在ARC中完全不允許。下面是一些解決方案:
1.用OC類代替c結(jié)構(gòu)。這應(yīng)該是最好的解決辦法。
?
2.如果使用OC類是次要的方法(可能你想要一個(gè)高密度的結(jié)構(gòu)數(shù)組),那么考慮使用void*代替。這需要使用顯示的轉(zhuǎn)化。
?
3.把對(duì)象引用作為__unsafe_unretained。這種方法是半常見的模式,這樣是有用的。
?
struct x { NSString *S;? int X; } StaticArray[] = {? ?
? @"foo", 42,? ?
? @"bar, 97,? ?
...? ?
};? ?
?
開發(fā)者這樣聲明
struct x { NSString * __unsafe_unretained S; int X; }? ?
這可能是有問題的,如果對(duì)象可以被釋放時(shí)指針是不安全的,但它是非常有用的東西,被稱為是字符串常量。
?
● 不能直接將id和void*進(jìn)行轉(zhuǎn)化(包含Core Foundation類型)參考Managing Toll-Free Bridging
?
經(jīng)常遇到的問題
?
我該怎樣認(rèn)識(shí)ARC?它在哪兒放置retains/releases。
?
不要想什么地方調(diào)用retain/release,考慮程序本身算法吧。考慮"strong and weak"關(guān)系,對(duì)象的所有權(quán)關(guān)系和可能存在的循環(huán)引用。
?
我還需要寫dealloc方法么
可能需要。因?yàn)锳RC不會(huì)自動(dòng)對(duì) Core Foundation 對(duì)象,文件描述符等等進(jìn)行malloc/free,,這些資源扔需要寫dealloc方法。
?
不不能release對(duì)象的變量,但是可以調(diào)用[self setDelegate:nil]在系統(tǒng)的類和其他不用ARC的代碼。
?
ARC種 不需要dealloc方法,或者[super dealloc];在運(yùn)行時(shí)系統(tǒng)調(diào)用super代碼。
?
在ARC種,仍存在循環(huán)引用?
是的。ARC自動(dòng)retain/release并可能繼承了循環(huán)引用的問題。幸運(yùn)的是:遷移到ARC的代碼很少泄露,因?yàn)闊o論properties是不是retain的,都被被聲明為retain了。
?
在ARC種blocks是如何工作的
Block能正常工作,當(dāng)你在棧上傳遞的時(shí)候,比如作為返回值。你無需block copy了。當(dāng)你傳遞block在棧下面的時(shí)候,添加到arrayWithObjects等需要retain的地方,需要使用[^{ }copy]。
?
有一件事情需要清除:NSString *__block myString是retain在ARC模式種,不可能是野指針。為了得到以前的行為,使用__block NSString *__unsafe_unretained myString 或者使用__block NSString *__weak myString.
?
我可以在雪豹OS種開發(fā)中使用ARC么?
不行。雪豹版本的Xcode4.2不支持ARC。
?
ARC下,可以創(chuàng)建C語言的retain的指針數(shù)組么
可以
?
// Note calloc() to get zero-filled memory.? ?
__strong SomeClass **dynamicArray = (__strong SomeClass **)calloc(entries, sizeof(SomeClass *));? ?
for (int i = 0; i < entries; i++) {? ?
???? dynamicArray[i] = [[SomeClass alloc] init];? ?
}? ?
?? ?
// When you're done, set each entry to nil to tell ARC to release the object.? ?
for (int i = 0; i < entries; i++) {? ?
???? dynamicArray[i] = nil;? ?
}? ?
free(dynamicArray);? ?
?
下面是一些注意的地方:
● 你要寫__strong SomeClass **在某些情況,默認(rèn)情況是__autoreleasing SomeClass **.
● 開辟的內(nèi)存必須是零填充
● 需要設(shè)置每一項(xiàng)為nil在釋放array的時(shí)候(memset和bzero不好使的)
?
ARC慢么?
取決于你怎么測(cè)量,通常是不慢。編譯器消除很多無關(guān)緊要的retain/release調(diào)用。投入很大的努力在加快OC下的運(yùn)行環(huán)境。尤其是返回一個(gè)retian/autoreleased對(duì)象,ARC并不是真正將它放到自動(dòng)釋放池。
?
有一件事需要清除:優(yōu)化器不在debug模式下。所以想看到更多的retain/release的調(diào)用,使用-O0比-Os
?
ARC在ObjC++模式下工作么?
是的,甚至可以將strong/weakids在類和容器中。ARC編譯器合成retain/release邏輯在拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù)鐘。
?
哪些類不支持weak引用?
下面的類不能創(chuàng)建弱引用:
NSATSTypesetter, NSColorSpace, NSFont, NSMenuView, NSParagraphStyle, NSSimpleHorizontalTypesetter, and NSTextView.
?
對(duì)于聲明properties時(shí),你應(yīng)該使用retain代替weak。對(duì)于變量你應(yīng)該使用__unsafe_unretained代替__weak.另外,你不能創(chuàng)建這些類的弱引用:NSHashTable, NSMapTable, or NSPointerArray
?
對(duì)NSCell和其他類使用NSCopyObject時(shí)候。
沒什么特別的。ARC負(fù)責(zé)的顯示retain的場(chǎng)景。ARC模式下,所有的copy方法應(yīng)該僅僅copy實(shí)例變量。
?
我可以對(duì)某個(gè)文件不使用ARC么?
?
是的。當(dāng)開發(fā)者遷移工程到ARC時(shí),對(duì)所有OC源文件設(shè)置為-fobjc-arc編譯選項(xiàng)。開發(fā)者可以指定一個(gè)文件設(shè)置-fno-objc-arc編譯選項(xiàng)。
?
GC(Grabage Collection)在mac上過時(shí)了?
Grabage Collection在10.8上已經(jīng)過時(shí),將來會(huì)重OS X中移除。ARC將是收推薦的替代技術(shù)。為了幫助遷移現(xiàn)有程序,ARC遷移工具在Xcode >=4.3 支持將GC遷移到ARC。
轉(zhuǎn)載于:https://www.cnblogs.com/lingzhao/p/3490899.html
總結(jié)
- 上一篇: 英国议会官方号关闭TikTok账户:刚注
- 下一篇: 对于.swp文件的恢复方法