{"id":1881,"date":"2012-02-22T16:37:00","date_gmt":"2012-02-22T08:37:00","guid":{"rendered":"http:\/\/localhost\/?p=93"},"modified":"2012-12-18T10:14:45","modified_gmt":"2012-12-18T02:14:45","slug":"object-c_properties_tab_assign_retain_copy_distinction","status":"publish","type":"post","link":"https:\/\/opgogo.com\/blog2\/?p=1881","title":{"rendered":"Object-C\u5c5e\u6027\u6807\u7b7eassign\uff0cretain\uff0ccopy\u533a\u522b"},"content":{"rendered":"<p>You usually need to claim ownership of your instance variables. See the Objective-C memory management rules. With a retain property, your property setter claims ownership of the new value and relinquishes ownership of the old one. With an assign property, the surrounding code has to do this, which is just as mess in terms of responsibilities and separation of concerns. The reason you would use an assign property is in a case where you can&#8217;t retain the value (such as non-object types like BOOL or NSRect) or when retaining it would cause unwanted side effects.<\/p>\n<p>Incidentally, in the case of an NSString, the correct kind of property is usually copy. That way it can&#8217;t change out from under you if somebody passes in an NSMutableString (which is valid \u2014 it is a kind of NSString).<\/p>\n<p>assign: Anything that is not an owning relationship should use assign rather than copy or retain. Paths down the object graph should be owning; paths up the graph (where included) should be non-owning. Otherwise, you will create circular references without having a delegate in the circle.<\/p>\n<p>retain or copy: You should use copy instead of retain for all classes that has a mutable variant. Eg. NSAArray, NSSet, NSDictionary, NSData, NSCharacterSet, NSIndexSet, and NSString.<\/p>\n<p>\u7ffb\u8bd1\uff1a<\/p>\n<p>\u4f60\u901a\u5e38\u4f1a\u58f0\u660e\u4f60\u7684\u5b9e\u4f8b\u53d8\u91cf\u7684\u6240\u6709\u6743\u3002\u770bObjective-C\u5185\u5b58\u7ba1\u7406\u89c4\u5219\uff1aretain\u5c5e\u6027\uff0c\u5c5e\u6027setter\u83b7\u5f97\u65b0\u503c\u7684\u6240\u6709\u6743\u548c\u653e\u5f03\u65e7\u503c\u7684\u6240\u6709\u6743\u3002assign\u5c5e\u6027\uff0c\u522b\u7684\u4ee3\u7801\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u8fd9\u53ea\u662f\u7406\u6e05\u8d23\u4efb\u548c\u533a\u5206\u5173\u6ce8\u70b9\u3002\u4f7f\u7528assign\u5c5e\u6027\u7684\u4e00\u79cd\u60c5\u51b5\u662f\u503c\u4e0d\u80fd\u88ab\u4fdd\u7559\uff08\u5982\u50cfBOOL\u6216NSRect\u975e\u5bf9\u8c61\u7c7b\u578b\uff09\uff0c\u6216\u4fdd\u7559\u5b83\u4f1a\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u526f\u4f5c\u7528\u3002<br \/>\n\u987a\u4fbf\u8bf4\u4e00\u4e0b\uff0c\u5728NSString\u7684\u60c5\u51b5\u4e0b\uff0c\u6b63\u786e\u7684\u5c5e\u6027\u901a\u5e38\u662fcopy\u3002\u8fd9\u6837\uff0c\u5982\u679c\u6709\u4eba\u4f20\u5165\u4e00\u4e2aNSMutableString\uff0c\u5c5e\u6027\u4f1a\u672a\u7ecf\u5141\u8bb8\u5c31\u88ab\u6539\u53d8\uff08\u8fd9\u662f\u6709\u6548\u7684 &#8211; NSMutableString\u4e5f\u662f\u4e00\u79cd\u7684NSString\uff09\u3002<br \/>\nassign\uff1a\u51e1\u662f\u4e0d\u662f\u62e5\u6709\u7684\u5173\u7cfb\uff0c\u5e94\u8be5\u4f7f\u7528assign\uff0c\u800c\u4e0d\u662fretain\u6216copy\u3002\u5728\u5bf9\u8c61\u56fe\u4e2d\u7684\u8def\u5f84\uff0c\u5411\u4e0b\u662f\u62e5\u6709\uff0c\u5411\u4e0a\u662f\u975e\u62e5\u6709\u3002\u5426\u5219\uff0c\u4f60\u53ef\u80fd\u521b\u5efa\u51fa\u5faa\u73af\u5f15\u7528\u3002<br \/>\nretain\u6216copy\uff1a\u5f53\u7c7b\u62e5\u6709mutable\u5b50\u7c7b\u65f6\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528copy\uff0c\u800c\u4e0d\u662fretain\u3002\u4f8b\u5982\uff1aNSAArray\uff0cNSSet\uff0cNSDictionary\uff0cNSData\u7684\uff0cNSCharacterSet\uff0cNSIndexSet\uff0cNSString\u3002<\/p>\n<p>\u53c2\u8003\uff1ahttp:\/\/stackoverflow.com\/questions\/1380338\/objective-c-101-retain-vs-assign-nsstring<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You usually need to claim ownership of your instance variables. See the Objective-C memory managemen&hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[28],"tags":[],"class_list":["post-1881","post","type-post","status-publish","format-standard","hentry","category-computer"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/posts\/1881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1881"}],"version-history":[{"count":0,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/posts\/1881\/revisions"}],"wp:attachment":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}