質問

Is NSMutableArray *myArray = [[NSMutableArray alloc] init]; any more or less correct than NSMutableArray *myArray = [NSMutableArray new]; in terms of style or efficiency?

役に立ちましたか?

解決

Actually both are used to initialize the new object. new will also do allocate and initialize. But the apple recommends that call alloc first, then after invoke the init method. Thats why we are using [[NSMutableArray alloc] init]

Anyway, there is no big difference here

他のヒント

Neither is "more correct," and in fact they are functionally equivalent, but alloc is much more common than new since custom initialization is always done through initWith...-style methods. So it's hard to say something is stylistically correct, but new is certainly less stylistically standard in modern Objective-C.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top