I was wondering if I can directly alloc, init an ivar now with ARC (Because if I remember well it wasn’t possible without ARC because it would leak memory no) ?
Example (consider an ivar declared like this) :
@property (nonatomic, strong) NSArray *myArray;
@synthetize myArray = _myArray;
And then do
self.myArray = [[NSArray alloc] initWithObjects:object1, object2, nil]; //I know I could directly use [NSArray arrayWithObjects:...] but that's the purpose of my question
Yes.
The only difference between using
alloc/initWithObjects:andarrayWithObjects:is that the former leaves you with a retained object, while the second doesn’t. If for some reason you wanted to use the former to initialize a property, you’d want to add a call toautoreleaseto avoid a leak. So you’d do this:You’re correct that with ARC, the compiler will take care of adding that for you, so you can now just say: