What does & # 8594; mean?

I'm a relative newbie to Objective-C (just studying the Arron Hillegras book) and am confused by the following code snippet that I found in one of the Apple code examples, in particular, what does β†’ mean mean?

-(id) initWithNumbers:(NSArray *)numbers { self = [super init]; if (self != nil) { self->_numbers = [numbers copy]; } return self; } 

In the header file, _numbers is declared as

 NSNumber * _number; 

(underlining has some meaning from what I remember reading somewhere, but this also eludes me at the moment.

Thanks Robin

+7
source share
5 answers

-> - the usual C operator for accessing the elements of a pointer to a structure; operator . designed to access members of the structure. Thus:

 a->b 

translates to

 (*a).b 

Since Objective-C objects are pointers to structures under it, this works to access instance variables.

+8
source

It is usually called the arrow operator. It allows you to access instance variables of an object (or struct ) using a reference or pointer to an instance. This is a common syntax with C and C ++.

I'm struggling to find a good record, but you can find this informative.

As for underscores, they usually mean "private"; in accordance with the Coding Rules for Cocoa - Naming Basics :

Avoid using the underscore character as a prefix meaning private, especially in methods. Apple Stocks use of this agreement. Use in third parties can lead to a collision of namespaces; they could involuntarily redefine the existing private method with one of their own, with disastrous consequences.

+2
source

You use -> when you want to access ivar. Like C structures, you will use . or -> (in pointers to structures) in Objective-C objects that you can use -> but are not needed, since you can access them directly.

Consequently:

 self->_numbers = [numbers copy]; 

and

 _numbers = [numbers copy]; 

are the same

You want to use -> if you want to explicitly access this ivar.

Remember that in Objective-C you can use . but only when this property. You can use -> no matter what.

+2
source

This indicates a reference to - in this case - the instance variable of the object. Self refers to the object itself, and by writing self->_numbers , you refer to a variable that is part of the class, instead of a global variable called _numbers .

Are you sure this is not mentioned in your book?

0
source

This is an operator that is rooted in the C programming language. And since Objective-C is compatible with C programming, you can see developers using traditional C programming.

"->" is used to access index items. And since the "objects" in Objective-C are just pointers (indicated by the *), for example. NSNumber * ;, you can use this notation to access your items.

I have never used this notation in Objective-C, since the dot notation in Objective-C is accepted. If "self" has the synthesized property "number", then event_number should be the same as self-> number (this is true ONLY in Objective-C.

0
source

All Articles