I define
@property (nonatomic, assign) int currentUserNum;
@property (nonatomic, assign) BOOL isAlive;
in @interface MyClass
and defined -init method in @implementation MyClass
@synthesize currentUserNum, isAlive;
-(id) init {
if (self = [super init]) {
self.currentUserNum = 0;
self.isAlive = YES;
}
return self;
}
self.currentUserNum = 0; is crashed , but self.isAlive = YES; can work ! They are both assign property.
I want to know why ? Thanks!
Your
initmethod is missing a lot of important code.Every
initmethod should follow this basic pattern. You assignselfthe value of calling an appropriatesuper initor otherself init. If that’s notnil, you then perform appropriate initialization code, and finally you returnself.