ARC鏄嚜iOS5涔嬪悗澧炲姞鐨勬柊鐗规€э紝瀹屽叏娑堥櫎浜嗘墜鍔ㄧ鐞嗗唴瀛樼殑鐑︾悙锛岀紪璇戝櫒浼氳嚜鍔ㄥ湪閫傚綋鐨勫湴鏂规彃"/>

Objective-C涓紝ARC涓嬬殑 strong鍜寃eak鎸囬拡鍘熺悊瑙i噴

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>

鎶€鏈垎浜? src=

閭d箞灏卞彲浠ヨ锛宯ameField鐨則ext灞炴€ф槸NSString瀵硅薄鐨勬寚閽堬紝涔熷氨鏄嫢鏈夎€咃紝璇ュ璞′繚瀛樹簡鏂囨湰杈撳叆妗嗙殑鍐呭銆?/p>

鎶€鏈垎浜? src=

 

2.濡傛灉鎵ц浜嗗涓嬩唬鐮?/p>

   NSString *name = self.nameField.text;  

涓€涓璞″彲浠ユ湁澶氫釜鎷ユ湁鑰?鍦ㄤ笂闈唬鐮佷腑,name鍙橀噺鍚屾牱涔熸槸杩欎釜NSString瀵硅薄鐨勬嫢鏈夎€?涔熷氨鏄湁涓や釜鎸囬拡鎸囧悜鍚屼竴涓璞?/p>

 

鎶€鏈垎浜? src=

3.闅忓悗鐢ㄦ埛鏀瑰彉浜嗚緭鍏ユ鐨勫唴瀹癸紝姣斿

鎶€鏈垎浜? src=

姝ゆ椂nameFeild鐨則ext灞炴€у氨鎸囧悜浜嗘柊鐨凬SString瀵硅薄銆備絾鍘熸潵鐨凬SString瀵硅薄浠嶇劧杩樻湁涓€涓墍鏈夎€?name鍙橀噺)锛屽洜姝や細缁х画淇濈暀鍦ㄥ唴瀛樹腑

鎶€鏈垎浜? src=

 

4.褰搉ame鍙橀噺鑾峰緱鏂板€?鎴栬€呬笉鍐嶅瓨鍦ㄦ椂(濡傚眬閮ㄥ彉閲忔柟娉曡繑鍥炴椂銆佸疄渚嬪彉閲忓璞¢噴鏀炬椂),鍘熷厛鐨凬SString瀵硅薄灏变笉鍐嶆嫢鏈変换浣曟墍鏈夎€?retain璁℃暟闄嶄负0,杩欐椂瀵硅薄浼氳閲婃斁

濡傦紝缁檔ame鍙橀噺璧嬩簣涓€涓柊鍊?/p>

銆€銆€name = @"Jake";  

鎶€鏈垎浜? src=

 

 

鎴戜滑绉皀ame鍜宯ameField.text鎸囬拡涓?Strong鎸囬拡"锛屽洜涓哄畠浠兘澶熶繚鎸佸璞$殑鐢熷懡銆傞粯璁ゆ墍鏈夊疄渚嬪彉閲忓拰灞€閮ㄥ彉閲忛兘鏄疭trong鎸囬拡銆?/p>

 

鍥涖€亀eak鎸囬拡

weak鍨嬬殑鎸囬拡鍙橀噺浠嶇劧鍙互鎸囧悜涓€涓璞★紝浣嗕笉灞炰簬瀵硅薄鐨勬嫢鏈夎€?/p>

1.鎵ц涓嬮潰鐨勪唬鐮?/p>   __weak NSString *name = self.nameField.text;  


鎶€鏈垎浜? src=

 

name鍙橀噺鍜宯ameField.text灞炴€ч兘鎸囧悜鍚屼竴涓狽SString瀵硅薄,浣唍ame涓嶆槸鎷ユ湁鑰?/p>

 

2.濡傛灉鏂囨湰妗嗙殑鍐呭鍙戠敓鍙樺寲,鍒欏師鍏堢殑NSString瀵硅薄灏辨病鏈夋嫢鏈夎€?浼氳閲婃斁,姝ゆ椂name鍙橀噺浼氳嚜鍔ㄥ彉鎴恘il,绉颁负绌烘寚閽?/p>

鎶€鏈垎浜? src=

weak鍨嬬殑鎸囬拡鍙橀噺鑷姩鍙樹负nil鏄潪甯告柟渚跨殑锛岃繖鏍烽樆姝簡weak鎸囬拡缁х画鎸囧悜宸查噴鏀惧璞★紝閬垮厤浜嗛噹鎸囬拡鐨勪骇鐢燂紝涓嶇劧浼氬鑷撮潪甯搁毦浜庡鎵剧殑Bug锛岀┖鎸囬拡娑堥櫎浜嗙被浼肩殑闂銆?/p>

 

3.weak鎸囬拡涓昏鐢ㄤ簬“鐖?瀛?rdquo;鍏崇郴锛岀埗浜叉嫢鏈変竴涓効瀛愮殑strong鎸囬拡锛屽洜姝ょ埗浜叉槸鍎垮瓙鐨勬墍鏈夎€咃紱浣嗕负浜嗛樆姝㈡墍鏈夋潈寰幆,鍎垮瓙闇€瑕佷娇鐢╳eak鎸囬拡鎸囧悜鐖朵翰銆傚吀鍨嬩緥瀛愭槸delegate妯″紡,浣犵殑ViewController閫氳繃strong鎸囬拡锛坰elf.view.subViews锛夋嫢鏈変竴涓猆ITableView, UITableView鐨刣ataSource鍜宒elegate閮芥槸weak鎸囬拡,鎸囧悜浣犵殑ViewController

鎶€鏈垎浜? src=

 

浜斻€乻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)鐨勬柟寮忎篃涓嶈;
2.鍙互鐢╠ealloc鏂规硶鏉ョ鐞嗕竴浜涜祫婧愶紝浣嗕笉鑳界敤鏉ラ噴鏀惧疄渚嬪彉閲忓[self.name release]锛屼篃涓嶈兘鍦╠ealloc鏂规硶閲岄潰鍘绘帀锛籹uper dealloc锛芥柟娉曪紝鍦ˋRC涓嬬埗绫荤殑dealloc鍚屾牱鐢辩紪璇戝櫒鏉ヨ嚜鍔ㄥ畬鎴愶紱
3.Core Foundation绫诲瀷鐨勫璞′粛鐒跺彲浠ョ敤CFRetain锛孋FRelease杩欎簺鏂规硶锛?/div>
4.涓嶈兘鍐嶄娇鐢∟SAllocateObject鍜孨SDeallocateObject瀵硅薄锛?/div>
5.涓嶈兘鍦–缁撴瀯浣撲腑浣跨敤瀵硅薄鎸囬拡锛屽鏋滄湁绫讳技鍔熻兘鍙互鍒涘缓涓€涓狾bjective锛岰绫绘潵绠$悊杩欎簺瀵硅薄锛?/div>
6.鍦╥d鍜寁oid锛婁箣闂存病鏈夌畝渚跨殑杞崲鏂规硶锛屽悓鏍峰湪Objective锛岰鍜孋ore Foundation绫诲瀷涔嬮棿鐨勮浆鎹㈤兘闇€瑕佷娇鐢ㄧ紪璇戝櫒鍒跺畾鐨勮浆鎹㈠嚱鏁帮紱
7.涓嶈兘鍐嶄娇鐢∟SAutoreleasePool瀵硅薄锛孉RC鎻愪緵浜咢autoreleasepool鍧楁潵浠f浛瀹冿紝杩欐牱鏇存湁鏁堢巼锛?/div>
8.涓嶈兘浣跨敤鍐呭瓨瀛樺偍鍖猴紙涓嶈兘鍐嶄娇鐢∟SZone锛夛紱
9.涓嶈兘浠ew涓哄紑澶寸粰涓€涓睘鎬у懡鍚嶏紱
10.澹版槑IBOutlet鏃朵竴鑸簲褰撲娇鐢╳eak锛?闄や簡瀵筍toryBoard杩欐牱nib涓棿鐨勯《灞傚璞¤鐢╯trong锛?/div>
11.weak鐩稿綋浜庤€佺増鏈殑assign锛宻trong鐩稿綋浜巖etain銆?/div>

以上是关于Objective-C涓紝ARC涓嬬殑 strong鍜寃eak鎸囬拡鍘熺悊瑙i噴的主要内容,如果未能解决你的问题,请参考以下文章

windows涓嬬殑mongodb鍒嗙墖閰嶇疆

涓嬭浇linux鎸囧畾鐩綍涓嬬殑鏂囦欢

鑷範瀹ょ鐞嗙郴缁燂紝鍩轰簬B/S妯″紡涓嬬殑JAVA绯荤粺

PHP鍩轰簬MVC妯″紡涓嬬殑鍖栧鍝侀攢鍞綉绔欙紝婧愮爜鍒嗕韩

PHP鍩轰簬MVC妯″紡涓嬬殑鍋滆溅鍦鸿溅浣嶇鐞嗙郴缁熴€?48婧愮爜涓嬭浇