why the * in front of the object name
I'm just starting to look at some objective-c stuff and was wondering what some of the content in this line of code means:
MyClass *myObject = [[MyClass alloc] init];
It's a bit confusin开发者_Go百科g why I need the asterisk. I understand that this is a pointer, but I've been reading some blogs that say this signifies that myObject is an object. What is the right way to think about that "*"?
The asterisk simply indicates that the myObject variable is a pointer, similar to its c counterpart.
Objective c objects are allocated on the heap, hence [[MyClass alloc] init] returns a pointer to a MyClass object. Hence, you need to declare a pointer variable to store the pointer.
Traditionally in C/C++, the * means "pointer to type of an object". So * int is "a pointer to an integer". So * MyObject is a type of "a pointer to a type of MyObject".
精彩评论