考察点 assign属性 和copy属性

@interface Person1 : NSObject //
@property (nonatomic, assign) int age;
@property (nonatomic, copy) NSString *name; //如果用assign会发生啥,

//两个方便的实现 对象相等 如何实现hash isEqual等函数
- (void)print;
@end

@implementation Person1
- (void)print{
    NSLog(@"%s",__func__);
}

//考察copy
//-(id)copyWithZone:(NSZone *)zone{
//    Person1 *p = [Person1 new];
//    p.name = self.name;
//    p.age = self.age;
//    return p;
//}
//- (id)mutableCopyWithZone:(NSZone *)zone{
//    Person1 *p = [Person1 new];
//    p.name = self.name;
//    p.age = self.age;
//    return p;
//}

@end

@interface ViewController ()
@property (nonatomic, assign) Person1 *person;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    Person1 *pers = [[Person1 alloc] init];
    self.person = pers;//pers 持有对象,所以没有释放
    self.person = [[Person1 alloc] init];  //assign赋值有直接释放了,会导致crash
    [self.person print];
    }
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.person print];
}
@end

你可能感兴趣的:(考察点 assign属性 和copy属性)