Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object declaration in Objective-C

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];
like image 962
Sahat Yalkabov Avatar asked Jul 03 '26 13:07

Sahat Yalkabov


1 Answers

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.

like image 57
Elfred Avatar answered Jul 06 '26 04:07

Elfred



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!