{"id":397,"date":"2015-01-06T11:51:54","date_gmt":"2015-01-06T03:51:54","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=397"},"modified":"2016-07-15T17:20:06","modified_gmt":"2016-07-15T09:20:06","slug":"atomic%e4%b8%8enonatomic%ef%bc%8cassigncopy%e4%b8%8eretain%e7%9a%84%e5%ae%9a%e4%b9%89%e5%92%8c%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2015\/01\/06\/115154397.html","title":{"rendered":"atomic\u4e0enonatomic\uff0cassign,copy\u4e0eretain\u7684\u5b9a\u4e49\u548c\u533a\u522b"},"content":{"rendered":"<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\"><strong>atomic\u548cnonatomic\u7528\u6765\u51b3\u5b9a\u7f16\u8bd1\u5668\u751f\u6210\u7684getter\u548csetter\u662f\u5426\u4e3a\u539f\u5b50\u64cd\u4f5c\u3002<br \/>\n<\/strong><\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\"><strong>? ?? ???atomic<\/strong><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\">? ?? ?? ?? ?? ? \u8bbe\u7f6e\u6210\u5458\u53d8\u91cf\u7684@property\u5c5e\u6027\u65f6\uff0c\u9ed8\u8ba4\u4e3aatomic\uff0c\u63d0\u4f9b\u591a\u7ebf\u7a0b\u5b89\u5168\u3002<\/span><\/span><\/p>\n<p><span style=\"color: #333333;\">? ?? ?? ?? ?? ? \u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u539f\u5b50\u64cd\u4f5c\u662f\u5fc5\u8981\u7684\uff0c\u5426\u5219\u6709\u53ef\u80fd\u5f15\u8d77\u9519\u8bef\u7684\u7ed3\u679c\u3002\u52a0\u4e86atomic\uff0csetter\u51fd\u6570\u4f1a\u53d8\u6210\u4e0b\u9762\u8fd9\u6837\uff1a<\/span><br \/>\n<span style=\"color: #333333;\">? ?? ?? ?? ?? ?? ?? ?? ?{lock}<\/span><br \/>\n<span style=\"color: #333333;\">? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???if (property != newValue) { <\/span><br \/>\n<span style=\"color: #333333;\">? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? [property release]; <\/span><br \/>\n<span style=\"color: #333333;\">? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? property = [newValue retain]; <\/span><br \/>\n<span style=\"color: #333333;\">? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}<\/span><br \/>\n<span style=\"color: #333333;\">? ?? ?? ?? ?? ?? ?? ?? ?{unlock}<\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\"><strong>? ?? ???nonatomic<\/strong><\/span><\/span><\/p>\n<p><span style=\"font-family: Arial;\">? ?? ???\u7981\u6b62\u591a\u7ebf\u7a0b\uff0c\u53d8\u91cf\u4fdd\u62a4\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\">? ?? ???atomic\u662fObjc\u4f7f\u7528\u7684\u4e00\u79cd\u7ebf\u7a0b\u4fdd\u62a4\u6280\u672f\uff0c\u57fa\u672c\u4e0a\u6765\u8bb2\uff0c\u662f\u9632\u6b62\u5728\u5199\u672a\u5b8c\u6210\u7684\u65f6\u5019\u88ab\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u8bfb\u53d6\uff0c\u9020\u6210\u6570\u636e\u9519\u8bef\u3002\u800c\u8fd9\u79cd\u673a\u5236\u662f\u8017\u8d39\u7cfb\u7edf\u8d44\u6e90\u7684\uff0c\u6240\u4ee5\u5728iPhone\u8fd9\u79cd\u5c0f\u578b\u8bbe\u5907\u4e0a\uff0c\u5982\u679c\u6ca1\u6709\u4f7f\u7528\u591a\u7ebf\u7a0b\u95f4\u7684\u901a\u8baf\u7f16\u7a0b\uff0c\u90a3\u4e48nonatomic\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u9009\u62e9\u3002<\/span><\/span><!--more--><br \/>\n<span style=\"color: #000000;\"><span style=\"font-family: Arial;\">? ?? ???\u6307\u51fa\u8bbf\u95ee\u5668\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u800c\u9ed8\u8ba4\u5730\uff0c\u8bbf\u95ee\u5668\u662f\u539f\u5b50\u64cd\u4f5c\u3002\u8fd9\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u89e3\u6790\u7684\u8bbf\u95ee\u5668\u63d0\u4f9b\u4e00\u4e2a\u5bf9\u5c5e\u6027\u7684\u5b89\u5168\u8bbf\u95ee\uff0c\u4ece\u83b7\u53d6\u5668\u5f97\u5230\u7684\u8fd4\u56de\u503c\u6216\u8005\u901a\u8fc7\u8bbe\u7f6e\u5668\u8bbe\u7f6e\u7684\u503c\u53ef\u4ee5\u4e00\u6b21\u5b8c\u6210\uff0c\u5373\u4fbf\u662f\u522b\u7684\u7ebf\u7a0b\u4e5f\u6b63\u5728\u5bf9\u5176\u8fdb\u884c\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u4e0d\u6307\u5b9a nonatomic \uff0c\u5728\u81ea\u5df1\u7ba1\u7406\u5185\u5b58\u7684\u73af\u5883\u4e2d\uff0c\u89e3\u6790\u7684\u8bbf\u95ee\u5668\u4fdd\u7559\u5e76\u81ea\u52a8\u91ca\u653e\u8fd4\u56de\u7684\u503c\uff0c\u5982\u679c\u6307\u5b9a\u4e86 nonatomic \uff0c\u90a3\u4e48\u8bbf\u95ee\u5668\u53ea\u662f\u7b80\u5355\u5730\u8fd4\u56de\u8fd9\u4e2a\u503c\u3002<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\"><strong>assign<\/strong><br \/>\n\u5bf9\u57fa\u7840\u6570\u636e\u7c7b\u578b \uff08NSInteger\uff0cCGFloat\uff09\u548cC\u6570\u636e\u7c7b\u578b\uff08int, float, double, char\uff09\u7b49\u7b49\u3002<br \/>\n\u6b64\u6807\u8bb0\u8bf4\u660e\u8bbe\u7f6e\u5668\u76f4\u63a5\u8fdb\u884c\u8d4b\u503c\uff0c\u8fd9\u4e5f\u662f\u9ed8\u8ba4\u503c\u3002\u5728\u4f7f\u7528\u5783\u573e\u6536\u96c6\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5982\u679c\u4f60\u8981\u4e00\u4e2a\u5c5e\u6027\u4f7f\u7528assign\uff0c\u4e14\u8fd9\u4e2a\u7c7b\u7b26\u5408NSCopying\u534f? ?? ?? ?? ? \u8bae\uff0c\u4f60\u5c31\u8981\u660e\u786e\u6307\u51fa\u8fd9\u4e2a\u6807\u8bb0\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u5730\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u5426\u5219\u7684\u8bdd\uff0c\u4f60\u5c06\u5f97\u5230\u4e00\u4e2a\u7f16\u8bd1\u8b66\u544a\u3002\u8fd9\u518d\u6b21\u5411\u7f16\u8bd1\u5668\u8bf4\u660e\u4f60\u786e\u5b9e\u9700\u8981\u8d4b\u503c\uff0c\u5373\u4f7f\u5b83\u662f? ?? ?? ???\u53ef\u62f7\u8d1d\u7684\u3002<\/p>\n<p><strong>retain<\/strong><br \/>\n\u5bf9\u5176\u4ed6NSObject\u548c\u5176\u5b50\u7c7b\u5bf9\u53c2\u6570\u8fdb\u884crelease\u65e7\u503c\uff0c\u518dretain\u65b0\u503c<br \/>\n\u6307\u5b9aretain\u4f1a\u5728\u8d4b\u503c\u65f6\u5524\u9192\u4f20\u5165\u503c\u7684retain\u6d88\u606f\u3002\u6b64\u5c5e\u6027\u53ea\u80fd\u7528\u4e8eObjective-C\u5bf9\u8c61\u7c7b\u578b\uff0c\u800c\u4e0d\u80fd\u7528\u4e8eCore Foundation\u5bf9\u8c61\u3002(\u539f\u56e0\u5f88\u660e\u663e\uff0cretain\u4f1a\u589e\u52a0\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u800c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6216\u8005Core Foundation\u5bf9\u8c61\u90fd\u6ca1\u6709\u5f15\u7528\u8ba1\u6570\u2014\u2014\u8bd1\u8005\u6ce8)\u3002<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\">? ?? ???\u6ce8\u610f: \u628a\u5bf9\u8c61\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\u65f6\uff0c\u5f15\u7528\u8ba1\u6570\u5c06\u589e\u52a0\u5bf9\u8c61\u7684\u5f15\u7528\u6b21\u6570+1\u3002<\/p>\n<p><strong>copy<\/strong><br \/>\n\u5bf9NSString \u5b83\u6307\u51fa\uff0c\u5728\u8d4b\u503c\u65f6\u4f7f\u7528\u4f20\u5165\u503c\u7684\u4e00\u4efd\u62f7\u8d1d\u3002\u62f7\u8d1d\u5de5\u4f5c\u7531copy\u65b9\u6cd5\u6267\u884c\uff0c\u6b64\u5c5e\u6027\u53ea\u5bf9\u90a3\u4e9b\u5b9e\u884c\u4e86NSCopying\u534f\u8bae\u7684\u5bf9\u8c61\u7c7b\u578b\u6709\u6548\u3002\u66f4\u6df1\u5165\u7684\u8ba8\u8bba\uff0c\u8bf7\u53c2\u8003\u201c\u590d\u5236\u201d\u90e8\u5206\u3002<br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\"><strong>copy\u4e0eretain<\/strong>\uff1a<br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\">Copy\u5176\u5b9e\u662f\u5efa\u7acb\u4e86\u4e00\u4e2a\u76f8\u540c\u7684\u5bf9\u8c61\uff0c\u800cretain\u4e0d\u662f\uff1a<br \/>\n1.\u6bd4\u5982\u4e00\u4e2aNSString \u5bf9\u8c61\uff0c\u5730\u5740\u4e3a0\u00d71111 \uff0c\u5185\u5bb9\u4e3a@\u201dSTR\u201d\uff0cCopy \u5230\u53e6\u5916\u4e00\u4e2aNSString \u4e4b\u540e\uff0c\u5730\u5740\u4e3a0\u00d72222 \uff0c\u5185\u5bb9\u76f8\u540c\u3002<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\">2.\u65b0\u7684\u5bf9\u8c61retain\u4e3a1 \uff0c\u65e7\u6709\u5bf9\u8c61\u6ca1\u6709\u53d8\u5316retain \u5230\u53e6\u5916\u4e00\u4e2aNSString \u4e4b\u540e\uff0c\u5730\u5740\u76f8\u540c\uff08\u5efa\u7acb\u4e00\u4e2a\u6307\u9488\uff0c\u6307\u9488\u62f7\u8d1d\uff09\uff0c\u5185\u5bb9\u5f53\u7136\u76f8\u540c\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u7684retain\u503c+1\u3002<br \/>\n\u603b\u7ed3\uff1aretain \u662f\u6307\u9488\u62f7\u8d1d\uff0ccopy \u662f\u5185\u5bb9\u62f7\u8d1d\u3002<br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\"><strong>assign\u4e0eretain<\/strong>\uff1a<br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Arial;\">1. \u63a5\u89e6\u8fc7C\uff0c\u90a3\u4e48\u5047\u8bbe\u4f60\u7528malloc\u5206\u914d\u4e86\u4e00\u5757\u5185\u5b58\uff0c\u5e76\u4e14\u628a\u5b83\u7684\u5730\u5740\u8d4b\u503c\u7ed9\u4e86\u6307\u9488a\uff0c\u540e\u6765\u4f60\u5e0c\u671b\u6307\u9488b\u4e5f\u5171\u4eab\u8fd9\u5757\u5185\u5b58\uff0c\u4e8e\u662f\u4f60\u53c8\u628aa\u8d4b\u503c\u7ed9\uff08assign\uff09\u4e86b\u3002\u6b64\u65f6a\u548cb\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\uff0c\u8bf7\u95ee\u5f53a\u4e0d\u518d\u9700\u8981\u8fd9\u5757\u5185\u5b58\uff0c\u80fd\u5426\u76f4\u63a5\u91ca\u653e\u5b83\uff1f\u7b54\u6848\u662f\u5426\u5b9a\u7684\uff0c\u56e0\u4e3aa\u5e76\u4e0d\u77e5\u9053b\u662f\u5426\u8fd8\u5728\u4f7f\u7528\u8fd9\u5757\u5185\u5b58\uff0c\u5982\u679ca\u91ca\u653e\u4e86\uff0c\u90a3\u4e48b\u5728\u4f7f\u7528\u8fd9\u5757\u5185\u5b58\u7684\u65f6\u5019\u4f1a\u5f15\u8d77\u7a0b\u5e8fcrash\u6389\u3002<br \/>\n2. \u4e86\u89e3\u52301\u4e2dassign\u7684\u95ee\u9898\uff0c\u90a3\u4e48\u5982\u4f55\u89e3\u51b3\uff1f\u6700\u7b80\u5355\u7684\u4e00\u4e2a\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528\u5f15\u7528\u8ba1\u6570\uff08reference counting\uff09\uff0c\u8fd8\u662f\u4e0a\u9762\u7684\u90a3\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u7ed9\u90a3\u5757\u5185\u5b58\u8bbe\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570\uff0c\u5f53\u5185\u5b58\u88ab\u5206\u914d\u5e76\u4e14\u8d4b\u503c\u7ed9a\u65f6\uff0c\u5f15\u7528\u8ba1\u6570\u662f1\u3002\u5f53\u628aa\u8d4b\u503c\u7ed9b\u65f6\u5f15\u7528\u8ba1\u6570\u589e\u52a0\u52302\u3002\u8fd9\u65f6\u5982\u679ca\u4e0d\u518d\u4f7f\u7528\u8fd9\u5757\u5185\u5b58\uff0c\u5b83\u53ea\u9700\u8981\u628a\u5f15\u7528\u8ba1\u6570\u51cf1\uff0c\u8868\u660e\u81ea\u5df1\u4e0d\u518d\u62e5\u6709\u8fd9\u5757\u5185\u5b58\u3002b\u4e0d\u518d\u4f7f\u7528\u8fd9\u5757\u5185\u5b58\u65f6\u4e5f\u628a\u5f15\u7528\u8ba1\u6570\u51cf1\u3002\u5f53\u5f15\u7528\u8ba1\u6570\u53d8\u4e3a0\u7684\u65f6\u5019\uff0c\u4ee3\u8868\u8be5\u5185\u5b58\u4e0d\u518d\u88ab\u4efb\u4f55\u6307\u9488\u6240\u5f15\u7528\uff0c\u7cfb\u7edf\u53ef\u4ee5\u628a\u5b83\u76f4\u63a5\u91ca\u653e\u6389\u3002<br \/>\n\u603b\u7ed3\uff1a\u4e0a\u9762\u4e24\u70b9\u5176\u5b9e\u5c31\u662fassign\u548cretain\u7684\u533a\u522b\uff0cassign\u5c31\u662f\u76f4\u63a5\u8d4b\u503c\uff0c\u4ece\u800c\u53ef\u80fd\u5f15\u8d771\u4e2d\u7684\u95ee\u9898\uff0c\u5f53\u6570\u636e\u4e3aint, float\u7b49\u539f\u751f\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528assign\u3002retain\u5c31\u59822\u4e2d\u6240\u8ff0\uff0c\u4f7f\u7528\u4e86\u5f15\u7528\u8ba1\u6570\uff0cretain\u5f15\u8d77\u5f15\u7528\u8ba1\u6570\u52a01, release\u5f15\u8d77\u5f15\u7528\u8ba1\u6570\u51cf1\uff0c\u5f53\u5f15\u7528\u8ba1\u6570\u4e3a0\u65f6\uff0cdealloc\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u5185\u5b58\u88ab\u56de\u6536\u3002<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>atomic\u548cnonatomic\u7528\u6765\u51b3\u5b9a\u7f16\u8bd1\u5668\u751f\u6210\u7684getter\u548csetter\u662f\u5426\u4e3a\u539f\u5b50\u64cd\u4f5c\u3002 ? ?? ???atomic ? ?? ?? ?? ?? ? \u8bbe\u7f6e\u6210\u5458\u53d8\u91cf\u7684@property\u5c5e\u6027\u65f6\uff0c\u9ed8\u8ba4\u4e3aatomic\uff0c\u63d0\u4f9b\u591a\u7ebf\u7a0b\u5b89\u5168\u3002 ? ?? ?? ?? ?? ? \u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u539f\u5b50\u64cd\u4f5c\u662f\u5fc5\u8981\u7684\uff0c\u5426\u5219\u6709\u53ef\u80fd\u5f15\u8d77\u9519\u8bef\u7684\u7ed3\u679c\u3002\u52a0\u4e86atomic\uff0csetter\u51fd\u6570\u4f1a\u53d8\u6210\u4e0b\u9762\u8fd9\u6837\uff1a ? ?? ?? ?? ?? ?? ?? ?? ?{lock} ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???if (property != newValue) { ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,11],"tags":[20],"class_list":["post-397","post","type-post","status-publish","format-standard","hentry","category-macos","category-project","tag-basics"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=397"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/397\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}