1. 程式人生 > >iOS ARC MRC情況下重寫setter getter方法

iOS ARC MRC情況下重寫setter getter方法

MRC:

@interface People : NSObject  
@property (nonatomic,retain) NSArray *oneArr;  
@end 
setter:
- (void)setOneArr:(NSArray *)oneArr{  
    if (_oneArr != oneArr) {  
        [oneArr release];  
        _oneArr = [oneArr retain];//或者copy視情況而定  
    }  
}  
getter:
- (NSArray *)oneArr{  
    if (!_oneArr) {  
        _oneArr = @[@"1"
,@"2",@"3"]; } return [[_oneArr retain] autorelease]; }

ARC:

@interface People : NSObject    
@property (nonatomic,strong) NSArray *twoArr;  
@end
setter:
- (void)setTwoArr:(NSArray *)twoArr{  
    if (_twoArr != twoArr) {  
        _twoArr = twoArr;  
    }  
} 
getter:  
- (NSArray
*)twoArr{ if (!_twoArr) { self.twoArr = @[@"1",@"2",@"3"]; } return _twoArr; }