Is there any difference in declaring objects in Objective-C between (1) and (2), besides the style and personal preference?
(1) One-line declaration, allocation, initialization.
Student *myStudent = [[Student alloc] init];
(2) Multi-line declaration, allocation, initialization.
Student *myStudent;
myStudent = [Student alloc];
myStudent = [myStudent init];
No, there isn't a difference. The [Student alloc] just allocates memory for a pointer, meanwhile [myStudent init] actually sets the initial values.
If you are familiar with C, think of alloc as doing
Student *myStudent = calloc(1, sizeof(Student));
And the init call as a function that sets the initial values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With