Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of -> in Cocoa

I'm used to create properties for pretty much everything. Probably too much, given. I also use instance variables, but I don't reference them with self->myvar but rather just myvar.

I recently was exposed to a code using a lot of self-> where I would have used a property. When I said one of the reasons I use @property (retain) is because I don't want to have to explicitly retain my object, I was told that I am "lazy". It's probably true, but it's also that I want to avoid bugs where I would have forgotten to retain.

Anyways, generally, what do you guys think about -> in Cocoa code?

like image 825
StuFF mc Avatar asked Jan 19 '11 14:01

StuFF mc


People also ask

Is it pronounced cacao or cocoa?

Cocoa (pronounced kow · kow) and cacao (pronounced ka · kaw) originate from the same place, the fruit of the Theobroma cacao tree.

What is end product of cocoa?

From the processed cocoa bean comes the fluid paste, or liquor, from which cocoa powder and chocolate are made. Chocolate is sold directly to the consumer as solid bars of eating chocolate, as packaged cocoa, and as baking chocolate.

What are the 5 uses of cocoa?

Long regarded as a food treat, cocoa is now used by some people as medicine. Cocoa seed is used for infectious intestinal diseases and diarrhea, asthma, bronchitis, and as an expectorant for lung congestion. The seed coat is used for liver, bladder, and kidney ailments; diabetes; as a tonic; and as a general remedy.


2 Answers

Laziness is a virtue.

I use -> in copyWithZone:, to access the copy's instance variables. (I don't use property accessors here for the same reasons I don't use them in init or dealloc — they may trigger side effects that would be bad in the half-initialized copy.) I also use self-> in this context because I like the way that reads:

other->foo = [self->foo copy];
other->bar = [self->bar copy];
other->baz = self->baz; //Non-object or object not owned (may be omitted entirely in the latter case)

I can't think of another context in which I use -> on objects.

like image 174
Peter Hosey Avatar answered Sep 28 '22 19:09

Peter Hosey


IMO there's not much value in using -> over plain ivar access. Sometimes it's useful for debugging purposes if you wanna access a different object's ivars. If it's just about differentiating ivars from local vars, you can use one of the established naming schemes (don't go into detail here) just as well.

The use of properties is a whole different story. IMO in most cases one should prefer using properties than plain ivars. Automatic retain is one reason, a good abstraction another.

like image 40
Ortwin Gentz Avatar answered Sep 28 '22 19:09

Ortwin Gentz