When allocating and initializing a new object (location), we usually do:
So obviously this calls the class to allocate memory, return the pointer, and initialize the memory at the pointer, and return the pointer again.
How come I see other code that does not do the allocation?
It would just go
and then go and do the fancy stuff with that.
Usually it is a variable within a method scope, and takes in the passed parameters from that method to work on something, before being returned as the method goes out of scope.
Can anyone please help explain?
Thanks!
Code:
NSString *myString = [[NSString alloc] init];
So obviously this calls the class to allocate memory, return the pointer, and initialize the memory at the pointer, and return the pointer again.
How come I see other code that does not do the allocation?
It would just go
Code:
NSString *newString;
and then go and do the fancy stuff with that.
Usually it is a variable within a method scope, and takes in the passed parameters from that method to work on something, before being returned as the method goes out of scope.
Can anyone please help explain?
Thanks!