Object declaration in Objective-C

Is there a difference in declaring objects in Objective-C between (1) and (2), in addition to style and personal preference?

(1) Single line declaration, allocation, initialization.

Student *myStudent = [[Student alloc] init];

(2) Multiline declaration, allocation, initialization.

Student *myStudent;
myStudent = [Student alloc]; 
myStudent = [myStudent init];
+3
source share
3 answers

In the second case, you can initialize the same object more than once. You send a message allocto the class to get an uninitialized instance that you must initialize, having several ways to do this:

NSString *myStr = [NSString alloc];
NSString *str1 = [myStr init]; //Empty string
NSString *str2 = [myStr initWithFormat:@"%@.%@", parentKeyPath, key];
+2
source

, . [Student alloc] , [myStudent init] .

C, ,

Student *myStudent = calloc(1, sizeof(Student));

init , .

+4

No, no difference.

+2
source

Source: https://habr.com/ru/post/1749341/


All Articles