高性能iOS应用开发 笔记03

2017-11-26  本文已影响5人  梓华

变量限定符

_strong
_weak 会被置nil
__unsafe_unretained 不会被置为nil
_autoreleasing

Person * _strong p1 = [[Person alloc] init];
Person * _weak p2 = [[Person alloc] init];
Person * __unsafe_unretained p3 = [[Person alloc] init];
Person * _autoreleasing p4 = [[Person alloc] init];

属性限定符

strong _strong
weak _weak
assign __unsafe_unretained
copy _strong
retain _strong
unsafe_unretained __unsafe_unretained

@property (nonatomic, strong) UILabel *lab;
@property (nonatomic, weak) id<UIApplicationDelegate> delegate;
@property (nonatomic, assign) NSInteger row;
@property (nonatomic, assign) BOOL flag;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) Person *person;
@property (nonatomic, unsafe_unretained) UIView *view;

Demo

- (IBAction)createStrongPhoto:(id)sender
{
    HPPhoto * __strong photo = [[HPPhoto alloc] init];
}

函数结束了 才会销毁

- (IBAction)createWeakPhoto:(id)sender
{
    HPPhoto * __weak wphoto = [[HPPhoto alloc] init];
}

对象立即被回收

- (void)createStrongToWeakPhoto:(id)sender
{
    HPPhoto * sphoto = [[HPPhoto alloc] init];
    HPPhoto * __weak wphoto = sphoto;
}

函数结束了 才会销毁

- (void)createUnsafeUnretainedPhoto:(id)sender
{
    HPPhoto * __unsafe_unretained wphoto = [[HPPhoto alloc] init];
}

对象会立即被释放

上一篇下一篇

猜你喜欢

热点阅读