Objective-C在ARC下,把strong指针设置为nil,请问这样不会不释放指针的内存空间?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Objective-C在ARC下,把strong指针设置为nil,请问这样不会不释放指针的内存空间?相关的知识,希望对你有一定的参考价值。
[[pointer alloc] init];
pointer = nil;
上面代码写错了,应该是:
Class1 *pointer = [[Class1 alloc] init];
pointer = nil;
Objective-C涓紝ARC涓嬬殑 strong鍜寃eak鎸囬拡鍘熺悊瑙i噴
鏍囩锛?/p>
Objective-C涓紝ARC涓嬬殑 strong鍜寃eak鎸囬拡鍘熺悊瑙i噴
鎻愮ず锛氭湰鏂囦腑鎵€璇寸殑"瀹炰緥鍙橀噺"鍗虫槸"鎴愬憳鍙橀噺"锛?灞€閮ㄥ彉閲?鍗虫槸"鏈湴鍙橀噺"
涓€銆佺畝浠?/strong>
ARC鏄嚜iOS 5涔嬪悗澧炲姞鐨勬柊鐗规€э紝瀹屽叏娑堥櫎浜嗘墜鍔ㄧ鐞嗗唴瀛樼殑鐑︾悙锛岀紪璇戝櫒浼氳嚜鍔ㄥ湪閫傚綋鐨勫湴鏂规彃鍏ラ€傚綋鐨剅etain銆乺elease銆乤utorelease璇彞銆備綘涓嶅啀闇€瑕佹媴蹇冨唴瀛樼鐞?鍥犱负缂栬瘧鍣ㄤ负浣犲鐞嗕簡涓€鍒囥€?/p>
娉ㄦ剰锛欰RC 鏄紪璇戝櫒鐗规€э紝鑰屼笉鏄?iOS 杩愯鏃剁壒鎬?闄や簡weak鎸囬拡绯荤粺)锛屽畠涔熶笉鏄被浼间簬鍏跺畠璇█涓殑鍨冨溇鏀堕泦鍣ㄣ€傚洜姝?ARC 鍜屾墜鍔ㄥ唴瀛樼鐞嗘€ц兘鏄竴鏍风殑锛屾湁鏃惰繕鑳芥洿鍔犲揩閫燂紝鍥犱负缂栬瘧鍣ㄨ繕鍙互鎵ц鏌愪簺浼樺寲
浜屻€佸師鐞?/strong>
ARC 鐨勮鍒欓潪甯哥畝鍗曪細鍙杩樻湁涓€涓彉閲忔寚鍚戝璞★紝瀵硅薄灏变細淇濇寔鍦ㄥ唴瀛樹腑銆傚綋鎸囬拡鎸囧悜鏂板€?鎴栬€呮寚閽堜笉鍐嶅瓨鍦ㄦ椂,鐩稿叧鑱旂殑瀵硅薄灏变細鑷姩閲婃斁銆傝繖鏉¤鍒欏浜庡疄渚嬪彉閲忋€乻ynthesize灞炴€с€佸眬閮ㄥ彉閲忛兘鏄€傜敤鐨?/p>
涓夈€乻trong鎸囬拡
鎺у埗鍣ㄤ腑鏈変釜鏂囨湰杈撳叆妗嗘灞炴€?/p>
@property (nonatomic, assign) IBOutlet UITextField *nameField;
1.濡傛灉鐢ㄦ埛鍦ㄦ枃鏈涓緭鍏j杩欎釜瀛楃涓?/p>
閭d箞灏卞彲浠ヨ锛宯ameField鐨則ext灞炴€ф槸NSString瀵硅薄鐨勬寚閽堬紝涔熷氨鏄嫢鏈夎€咃紝璇ュ璞′繚瀛樹簡鏂囨湰杈撳叆妗嗙殑鍐呭銆?/p>
2.濡傛灉鎵ц浜嗗涓嬩唬鐮?/p>
NSString *name = self.nameField.text;
涓€涓璞″彲浠ユ湁澶氫釜鎷ユ湁鑰?鍦ㄤ笂闈唬鐮佷腑,name鍙橀噺鍚屾牱涔熸槸杩欎釜NSString瀵硅薄鐨勬嫢鏈夎€?涔熷氨鏄湁涓や釜鎸囬拡鎸囧悜鍚屼竴涓璞?/p>
3.闅忓悗鐢ㄦ埛鏀瑰彉浜嗚緭鍏ユ鐨勫唴瀹癸紝姣斿
姝ゆ椂nameFeild鐨則ext灞炴€у氨鎸囧悜浜嗘柊鐨凬SString瀵硅薄銆備絾鍘熸潵鐨凬SString瀵硅薄浠嶇劧杩樻湁涓€涓墍鏈夎€?name鍙橀噺)锛屽洜姝や細缁х画淇濈暀鍦ㄥ唴瀛樹腑
4.褰搉ame鍙橀噺鑾峰緱鏂板€?鎴栬€呬笉鍐嶅瓨鍦ㄦ椂(濡傚眬閮ㄥ彉閲忔柟娉曡繑鍥炴椂銆佸疄渚嬪彉閲忓璞¢噴鏀炬椂),鍘熷厛鐨凬SString瀵硅薄灏变笉鍐嶆嫢鏈変换浣曟墍鏈夎€?retain璁℃暟闄嶄负0,杩欐椂瀵硅薄浼氳閲婃斁
濡傦紝缁檔ame鍙橀噺璧嬩簣涓€涓柊鍊?/p>
銆€銆€name = @"Jake";
鎴戜滑绉皀ame鍜宯ameField.text鎸囬拡涓?Strong鎸囬拡"锛屽洜涓哄畠浠兘澶熶繚鎸佸璞$殑鐢熷懡銆傞粯璁ゆ墍鏈夊疄渚嬪彉閲忓拰灞€閮ㄥ彉閲忛兘鏄疭trong鎸囬拡銆?/p>
鍥涖€亀eak鎸囬拡
weak鍨嬬殑鎸囬拡鍙橀噺浠嶇劧鍙互鎸囧悜涓€涓璞★紝浣嗕笉灞炰簬瀵硅薄鐨勬嫢鏈夎€?/p>
1.鎵ц涓嬮潰鐨勪唬鐮?/p> __weak NSString *name = self.nameField.text;
name鍙橀噺鍜宯ameField.text灞炴€ч兘鎸囧悜鍚屼竴涓狽SString瀵硅薄,浣唍ame涓嶆槸鎷ユ湁鑰?/p>
2.濡傛灉鏂囨湰妗嗙殑鍐呭鍙戠敓鍙樺寲,鍒欏師鍏堢殑NSString瀵硅薄灏辨病鏈夋嫢鏈夎€?浼氳閲婃斁,姝ゆ椂name鍙橀噺浼氳嚜鍔ㄥ彉鎴恘il,绉颁负绌烘寚閽?/p>
weak鍨嬬殑鎸囬拡鍙橀噺鑷姩鍙樹负nil鏄潪甯告柟渚跨殑锛岃繖鏍烽樆姝簡weak鎸囬拡缁х画鎸囧悜宸查噴鏀惧璞★紝閬垮厤浜嗛噹鎸囬拡鐨勪骇鐢燂紝涓嶇劧浼氬鑷撮潪甯搁毦浜庡鎵剧殑Bug锛岀┖鎸囬拡娑堥櫎浜嗙被浼肩殑闂銆?/p>
3.weak鎸囬拡涓昏鐢ㄤ簬“鐖?瀛?rdquo;鍏崇郴锛岀埗浜叉嫢鏈変竴涓効瀛愮殑strong鎸囬拡锛屽洜姝ょ埗浜叉槸鍎垮瓙鐨勬墍鏈夎€咃紱浣嗕负浜嗛樆姝㈡墍鏈夋潈寰幆,鍎垮瓙闇€瑕佷娇鐢╳eak鎸囬拡鎸囧悜鐖朵翰銆傚吀鍨嬩緥瀛愭槸delegate妯″紡,浣犵殑ViewController閫氳繃strong鎸囬拡锛坰elf.view.subViews锛夋嫢鏈変竴涓猆ITableView, UITableView鐨刣ataSource鍜宒elegate閮芥槸weak鎸囬拡,鎸囧悜浣犵殑ViewController
浜斻€乻trong鍜寃eak鎸囬拡鐨勪娇鐢ㄦ敞鎰?/strong>
1.涓嬮潰浠g爜鏄湁闂鐨?
銆€銆€__weak NSString *str = [[NSString alloc] initWithFormat:@"1234"];
銆€銆€NSLog(@"%@", str); // 鎵撳嵃鍑烘潵鏄?(null)"
str鏄釜weak鎸囬拡锛屾墍浠SString瀵硅薄娌℃湁鎷ユ湁鑰?鍦ㄥ垱寤轰箣鍚庡氨浼氳绔嬪嵆閲婃斁銆俋code杩樹細缁欏嚭璀﹀憡("Warning: Assigning retained object to weak variable; object will be released after assignment")
2.涓€鑸殑鎸囬拡鍙橀噺榛樿灏辨槸strong绫诲瀷鐨?鍥犳涓€鑸垜浠浜巗trong鍙橀噺涓嶅姞__strong淇グ,浠ヤ笅涓よ浠g爜鏄瓑浠风殑:
銆€銆€NSString *name = self.nameField.text;
銆€銆€__strong NSString *name = self.nameField.text;
3.灞炴€у彲浠ユ槸strong鎴杦eak,鍐欐硶濡備笅
銆€銆€@property (nonatomic, strong) NSString *name; 銆€銆€
銆€銆€@property (nonatomic, weak) id delegate;
4.浠ヤ笅浠g爜鍦ˋRC涔嬪墠鏄彲鑳戒細琛屼笉閫氱殑,鍥犱负鍦ㄦ墜鍔ㄥ唴瀛樼鐞嗕腑,浠嶯SArray涓Щ闄や竴涓璞℃椂,杩欎釜瀵硅薄浼氬彂閫佷竴鏉elease娑堟伅锛屽彲鑳戒細琚珛鍗抽噴鏀俱€傞殢鍚嶯SLog()鎵撳嵃璇ュ璞″氨浼氬鑷村簲鐢ㄥ穿婧冦€?闈濧RC,闇€鍐欎綔 id obj = [[array objectAtIndex:0] retain]; )
銆€銆€id obj = [array objectAtIndex:0]; //浠庢暟缁刟rray涓彇鍑虹储寮曚负0鐨勫璞★紝鐢╫bj鍙橀噺鏉ユ帴鏀躲€?/span>
銆€銆€[array removeObjectAtIndex:0]; //璁゛rray灏嗙储寮曚负0鐨勫璞$Щ闄ゃ€?闈濧RC,鍔犲叆array鐨勫璞★紝鎵€鏈夋潈浜ょ敱鏁扮粍鏉ョ鐞?
銆€銆€NSLog(@"%@", obj);
鍦ˋRC涓繖娈典唬鐮佹槸瀹屽叏鍚堟硶鐨?鍥犱负obj鍙橀噺鏄竴涓猻trong鎸囬拡,瀹冩垚涓轰簡瀵硅薄鐨勬嫢鏈夎€?浠嶯SArray涓Щ闄よ瀵硅薄涔熶笉浼氬鑷村璞¤閲婃斁銆?/p>
鍏€丄RC灏忕粨
1.鏈変簡ARC,鎴戜滑鐨勪唬鐮佸彲浠ユ竻鏅板緢澶?浣犱笉鍐嶉渶瑕佽€冭檻浠€涔堟椂鍊檙etain鎴杛elease瀵硅薄銆傚敮涓€闇€瑕佽€冭檻鐨勬槸瀵硅薄涔嬮棿鐨勫叧鑱?涔熷氨鏄摢涓璞℃嫢鏈夊摢涓璞?
2.ARC涔熸湁涓€浜涢檺鍒讹細
1> 棣栧厛ARC鍙兘宸ヤ綔浜嶰bjective-C瀵硅薄,濡傛灉搴旂敤浣跨敤浜咰ore Foundation鎴杕alloc()/free(),姝ゆ椂杩樻槸闇€瑕佷綘鏉ユ墜鍔ㄧ鐞嗗唴瀛?/p>
2> 姝ゅARC杩樻湁鍏跺畠涓€浜涙洿涓轰弗鏍肩殑璇█瑙勫垯,浠ョ‘淇滱RC鑳藉姝e父鍦板伐浣?/p>
3.铏界劧ARC绠$悊浜唕etain鍜宺elease,浣嗗苟涓嶈〃绀轰綘瀹屽叏涓嶉渶瑕佸叧蹇冨唴瀛樼鐞嗙殑闂銆傚洜涓簊trong鎸囬拡浼氫繚鎸佸璞$殑鐢熷懡,鏌愪簺鎯呭喌涓嬩綘浠嶇劧闇€瑕佹墜鍔ㄨ缃繖浜涙寚閽堜负nil,鍚﹀垯鍙兘瀵艰嚧搴旂敤鍐呭瓨涓嶈冻銆傛棤璁轰綍鏃朵綘鍒涘缓涓€涓柊瀵硅薄鏃?閮介渶瑕佽€冭檻璋佹嫢鏈夎瀵硅薄,浠ュ強杩欎釜瀵硅薄闇€瑕佸瓨娲诲涔?/p>
4.ARC杩樿兘寰堝ソ鍦扮粨鍚圕++浣跨敤,杩欏娓告垙寮€鍙戞槸闈炲父鏈夊府鍔╃殑銆傚浜巌OS 4,ARC鏈変竴鐐圭偣闄愬埗(涓嶆敮鎸亀eak鎸囬拡),浣嗕篃娌″お澶у叧绯?/p>
涓冦€丄RC浣跨敤娉ㄦ剰鎬荤粨
1.涓嶈兘鐩存帴璋冪敤dealloc鏂规硶(涔熶笉鑳藉湪dealloc鏂规硶閲岄潰鍘昏皟[super dealloc]鏂规硶)锛屼笉鑳借皟鐢╮etain锛宺elease锛宎utorelease锛宺etainCount鏂规硶锛屽寘鎷珸selector(retain)鐨勬柟寮忎篃涓嶈;以上是关于Objective-C在ARC下,把strong指针设置为nil,请问这样不会不释放指针的内存空间?的主要内容,如果未能解决你的问题,请参考以下文章
Objective-C涓紝ARC涓嬬殑 strong鍜寃eak鎸囬拡鍘熺悊瑙i噴
我应该在接口中声明变量还是在objective-c arc中使用属性?