Using Asterisk in Objective-C

I have a question regarding the use of asterisks in Objective-C. Just to be clear: I understand what pointers are and everything in procedural C. I was interested in two things:

1) Why are all pointers to objects (links to) Objective-C? Why not simple variables? (ie array NSArray = [[NSArray alloc] init];)

2) Why do you omit the asterisk when calling a method?

Thank!

+3
source share
2 answers

1) Why are all (references) Objective-C pointers to objects? Why aren't there simple variables? (ie array NSArray = [[NSArray alloc] init];)

Think of an Objective-C object as an illustrious structure for a moment.

NSArray array; , . NSArray *array; , , .

, , , Objective-C . , ( , -retain - ).

Objective-C C , ++, C, *, .

, id , id . , Objective-C . Snow Leopard Objective-C, . , .

2) , ?

. ...

[anArray objectAtIndex: 5];

... ...

objc_msgSend(anArray, @selector(objectAtIndex:), 5);

... ....

- (id) objectAtIndex: (NSUInteger) anIndex;

... C...

id object_at_index(id self, SEL _cmd, NSUInteger anIndex) { ... }

, Objective-C - C, . , , " ":

NSLog(@"method %@", NSStringFromSelector(_cmd));
+20

Objective-C , , . , .
,

NSArray array = [[NSArray alloc] init];  

, NSArray .

+2

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


All Articles