is there difference between initializations [nsarray new] , [nsarray array]?
array seems part of implementation of nsarray while new belongs nsobject.
new = alloc + init
this method combination of alloc , init. alloc, initializes isa instance variable of new object points class data structure. invokes init method complete initialization process.
+new implemented quite literally as:
+ (id) new { return [[self alloc] init]; } and new doesn't support custom initializers (like initwithobjects), alloc + init more explicit new
so question about:
[nsarray array] vs [[nsarray alloc] init]
the main difference between these if you're not using arc (automatic reference counting). first 1 returns retained , autoreleased object. second 1 returns object retained. in first case, want retain if wanted keep around longer current run loop. in second case, want release or autorelease if didn't want keep around.
now have arc, changes things. basically, in arc code, doesn't matter of these use.
but keep in mind [nsarray array] returns empty immutable array, using array nsmutablearray makes more sense
for more information:
alloc, init, , new in objective-c
use of alloc init instead of new
difference between [nsmutablearray array] vs [[nsmutablearray alloc] init]
Comments
Post a Comment