i have model example product. product has required property id. thought model can't without property. implementation of init method product class.
- (instancetype)initwithproductid:(nsnumber *)productid { if ([productid integervalue] <= 0) { return nil; } self = [super init]; if (self) { _productid = productid; } return self; } is legal return nil inside initilization methods? memory management problems? , 1 explain why it's legal , when should use it.
almost. has called alloc create unitialised object, , calling init method. code, super class init not called. when object deallocated, super class dealloc called. might fail if superclass init never called, depending on implementation of superclass. correct way be:
if ((self = [super init]) != nil) { if (productid.integervalue <= 0) return nil; _productid = productid; } return self;
Comments
Post a Comment