IOS?????property???
???????????? ???????[ 2014/11/21 14:50:04 ] ??????????????? ?????? iOS
???????????????????У???????????á?why?????????????????????????@property?????????????????@prperty????????????????????????????????????????????????getter??setter??????????????????property(nonatomic??copy)??????????copy????????????????????
????_name = [name copy];
????????????retain??????assign??????????????????
????//property(retain)NSString* name;
????_name = [name retain];
????//property(assign)NSString* name;
????_name = name;
???????????????????????????????@property??????????????getter??setter??????????????????????????????????????????????@property???????????£?????????????????????????????????????????????????????????????????????????????????????????????????????????????????@property??????????
??????????????atomic ??nonatomic?????????÷??????????????????????δ???
????@property(nonatomic?? retain) UITextField *userName; //1
????@property(nonatomic?? retain??readwrite) UITextField *userName; //2
????@property(atomic?? retain) UITextField *userName; //3
????@property(retain) UITextField *userName; //4
????@property(atomic??assign) int i; // 5
????@property(atomic) int i; //6
????@property int i; //7
????????????????????????????????????
????????????1??2??????3??4??????5??6??7??????????atomic??????????assign??????????readwrite????????????????????д??@property(nontomic)NSString *name;?????????????棬???????
??????????gc?????????????assign???η?????е????????????assign?????????retain??copy????????????NSString??copy??delegate??assign??????????assign???????????????????????????????????????objc?????????????int??float???????????????assign???????assign??????????objc?????????NSArray??NSDate??retain??
???????????????????伸???????????????????????Щ??????setter????????????????????????????getter??????????????????????????????????????.m????????????setter/getter????????????????????????????????????getter/setter????????????
//???????
@interface BaseClass : NSObject{
@public
NSString *_name;
}
@property(nonatomic??copy??readonly) NSString *name; //??????????readonly?????л?????geter????
-(void) setName:(NSString*)newName;
//???????
@interface BaseClass : NSObject{
@public
NSString *_name;
}
@property(nonatomic??copy??readonly) NSString *name; //?????????????readonly?????????????getter????????????????????????getter??????
-(NSString*) name; //getter??????????????name?????????????Foundation.framework?????UIView.h???????????property???????
-(void) setName:(NSString*)newName;
//????????
@interface BaseClass : NSObject{
@public
NSString *_name;
}
@property(nonatomic??copy??readwrite) NSString *name; //?????????????????????getter??setter
//???????
@interface BaseClass : NSObject{
@public
NSString *_name;
}
@property(nonatomic??copy) NSString *name; //???readwrite?????????????????????
??????????δ?????????????????????????δ???
//???????
@synthesize name = _name; //??仰????????????????ж????κ?getter??setter?????????????????getter??setter
//???????
@synthesize name = _name; //?????????name?????getter???????????????????????setter?????????setName??????
-(NSString*) name{
NSLog(@"name");
return _name;
}
//????????
@synthesize name = _name; //?????????????setter?????????????????????????getter????
-(void) setName:(NSString *)name{
NSLog(@"setName");
if (_name != name) {
[_name release];
_name = [name copy];
}
}
//???????
@synthesize name = _name; //???????????????getter??setter????仰????????????????
-(NSString*) name{
NSLog(@"name");
return _name;
}
-(void) setName:(NSString *)name{
NSLog(@"setName");
if (_name != name) {
[_name release];
_name = [name copy];
}
}
????????????δ??????????????????????Property????????????и??????????????ɡ???????????С?????????????Property???????????????д??setter/getter????????????????????????????Щ???????д????????????????????????????????
//BaseClass.h
@interface BaseClass : NSObject{
@public
NSArray *_names;
}
@property(nonatomic??assgin??readonly) NSArray *names; //?????????assign
-(void) setNames:(NSArray*)names;
//BaseClass.m
@implementation BaseClass
@synthesize names = _names;
-(NSArray*) names{
NSLog(@"names");
return _names;
}
-(void) setNames:(NSArray*)names{
NSLog(@"setNames");
if (_name != name) {
[_name release];
_name = [name retain]; //??retain????????????????????????????????????setNames?????????????????assign
}
}
?????????????@property?????????????????????????????£??????????????getter??setter?????atomic/nonatomic/retain/assign/copy??Щ???????????飬??????????????????????????????????????getter??setter??????????????????????У????????????atomic/nonatomic/retain/assign/copy????????????????????????????????getter??setter??
?????????????????????????????ɡ?atomic??nonatomic????????????
???????????@synthesize??????????????????atomic??nonatomic???????????????????????atomic??????????????????getter??setter?????????????????????????????????????????get?????set?????????????????????????????????????????????????????atomic????????????????????д???????????????????????????????
??????nonatomic????????“?????”??????????????????????????????????????????????nonatomic??atomic???????????????????????????????property?????????????nonatomic???
????????????????ж????????????????????dealloc???????????????????self.xxx = nil;????????????????????????д?????????????????[xxx release]; xxx = [nil retain];(---??????property(nonatomic??retian)xxx?????????????????????????????????
???????nil????????????κ???????????????????release??????????nil????????c?????????????????????ɡ?
????int* arr = new int[10]; ?????????delete arr; arr = NULL; ??objc???????????仰self.arr = nil;????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11