{"id":1553,"date":"2013-06-09T15:55:02","date_gmt":"2013-06-09T07:55:02","guid":{"rendered":"http:\/\/me.doucl.com\/?p=1553"},"modified":"2013-06-09T15:57:35","modified_gmt":"2013-06-09T07:57:35","slug":"object-c-property","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/mobile\/object-c-property.html","title":{"rendered":"Object-C \u5c5e\u6027\u4fee\u9970\u7b26\u4f7f\u7528\u7965\u89e3"},"content":{"rendered":"
Object-C\u5c5e\u6027\u5b9a\u4e49@property\u4fee\u9970\u7b26\u4e3b\u8981\u6709<\/p>\n
getter=getterName \u548c setter=setterName, \u663e\u5f0f\u8bbe\u7f6e getter\/setter \u65b9\u6cd5\u540d, \u672a\u6307\u5b9a\u5b83\u4eec\u65f6 Objective-C \u4f1a\u4e3a\u6211\u4eec\u751f\u6210\u9ed8\u8ba4\u7684 setter\/getter \u65b9\u6cd5, \u6709\u4e00\u5b9a\u7684\u89c4\u5219\uff0c\r\n\u6bd4\u5982\u4e0a\u9762\u7684 NSString* gender \u5c5e\u6027\u751f\u6210\u9ed8\u8ba4\u7684setter \u65b9\u6cd5\u662f: -(void) setGender:(NSString *);\r\ngetter \u65b9\u6cd5\u662f: -(NSString *) gender;\r\n\u60f3\u770b\u770b Objective-C \u4e3a\u6211\u4eec\u751f\u6210\u4ec0\u4e48 getter\/setter \u65b9\u6cd5, \u4e0d\u7528\u70b9\u53f7\u6765\u9690\u5f0f\u8c03\u7528 setter\/getter \u65b9\u6cd5\uff0c\u800c\u662f\u663e\u5f0f\u7684\u7528 [unmi setGender] \u6216 [unmi gender], \u8f93\u5165\u5f0f\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u4f1a\u81ea\u52a8\u63d0\u793a\u51fa\u6765\u7684\u3002\u6ce8\u610f\u8fd9\u91cc\u7684 getter \u65b9\u6cd5\u540d\u5e76\u975e\u662f\u50cf Java \u7684 getGender, \u800c\u662f\u548c\u5c5e\u6027\u540d\u540c.\r\n\u5047\u5982\u4f60\u60f3\u8981\u81ea\u5df1\u4e2a\u6027\u7684 getter\/setter \u65b9\u6cd5\uff0c\u6bd4\u5982\u5199\u6210 @property(getter=getGender, setter=setSex:) NSString* gender; \u90a3\u4e48\u76f8\u5e94\u7684\u5c31\u4f1a\u751f\u6210\uff1a\r\n setter \u65b9\u6cd5\u662f: -(void) setSex:(NSString *);\r\n getter \u65b9\u6cd5\u662f: -(NSString *) getGender;\r\n\u5728 Xcode \u4e2d esc unmi \u5c31\u80fd\u770b\u5230\u76f8\u5e94\u7684 setter\/getter \u65b9\u6cd5\u540d\u7684. \u53ef\u4ee5\u53ea\u7528\u5176\u4e2d\u4e00\u4e2a\u4e86\uff0c\u90a3\u53e6\u4e00\u4e2a\u4fdd\u6301\u9ed8\u8ba4\u3002\u8fd9\u4e24\u4e2a\u8f83\u5c11\u7528\uff0c\u7528\u9014\u5c31\u662f\u53ef\u7528\u6765\u751f\u6210\u81ea\u5df1\u4e2a\u6027\u7684\u4f46\u8981\u7b26\u5408\u67d0\u4e2a\u8303\u56f4\u5185\u89c4\u8303\u7684 setter\/getter \u65b9\u6cd5\u3002\u50cf @property(getter = isOnline) BOOL online; \u5219\u4f1a\u751f\u6210 -(BOOL) isOnline; \u8fd9\u6837\u7684 getter \u65b9\u6cd5\uff0c\u800c\u4e0d\u662f -(BOOL) online; \u5f53\u7136\u6211\u4eec\u4e5f\u5f88\u5c11\u4e14\u4e0d\u63a8\u8350\u76f4\u63a5\u8c03\u7528 getter\/setter \u65b9\u6cd5\uff0c\u800c\u662f\u7528\u70b9\u53f7\u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u6709\u610f\u601d\u7684\u53bb\u8986\u76d6 getter\/setter \u65b9\u6cd5\u65f6\u5c31\u8f83\u8c61\u660e\u786e\u4e86\u3002<\/pre>\n<\/li>\n<\/ul>\n\u90a3\u4e48\u4ec0\u4e48\u65f6\u5019\u7528assign\u3001\u4ec0\u4e48\u65f6\u5019\u7528retain\u548ccopy\u5462\uff1f
\n\u63a8\u8350\u505a\u6cd5\u662fNSString\u7528copy,delegate\u7528assign\uff08\u4e14\u4e00\u5b9a\u8981\u7528assign\uff0c\u4e0d\u8981\u95ee\u4e3a\u4ec0\u4e48\uff0c\u53ea\u7ba1\u53bb\u7528\u5c31\u662f\u4e86\uff0c\u4ee5\u540e\u4f60\u4f1a\u660e\u767d\u7684\uff09\uff0c\u975eobjc\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982int\uff0cfloat\u7b49\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7528assign\uff08\u9ed8\u8ba4\u5c31\u662fassign\uff09\uff0c\u800c\u5176\u5b83objc\u7c7b\u578b\uff0c\u6bd4\u5982NSArray\uff0cNSDate\u7528retain\u3002<\/p>\n\u987a\u4fbf\u8bf4\u660e\u4e00\u4e0b@property \u4e2d\u7684 retain\/assign\/copy \u5bf9\u5e94 setter \u65b9\u6cd5\u7684\u5185\u90e8\u5b9e\u73b0\uff1a<\/p>\n
\r\n\/\/retain \u65f6\uff1a\r\n-(void) setAddress: (Address *) address {\r\n if(_address){\r\n [_address release];\r\n }\r\n _address = [address retain];\r\n } \r\n\r\n\/\/assign \u65f6\uff1a\r\n -(void) setAddress: (Address *) address {\r\n self.address = address;\r\n } \r\n\r\n\/\/copy \u65f6\uff1a\r\n -(void) setAddress: (Address *) address {\r\n if(_address){\r\n [_address release];\r\n }\r\n self.address = [address copyWithZone: zone];\/\/ Address \u5fc5\u987b\u5b9e\u73b0 NSCoping \u534f\u8bae\r\n }<\/pre>\n<\/div>\n
\n\u53c2\u8003\u8d44\u6599
\n<\/strong>https:\/\/developer.apple.com\/library\/mac\/#documentation\/Cocoa\/Conceptual\/ObjCRuntimeGuide\/Articles\/ocrtPropertyIntrospection.html
\nhttp:\/\/www.cnblogs.com\/zilongshanren\/archive\/2011\/08\/03\/2125728.html<\/p>\n