Using & # 8594; in cocoa

I use to create properties for almost everything. Probably too much, considering. I also use instance variables, but I do not reference them with self->myvar , but just myvar .

I was recently introduced to code using a lot of self-> where I would use a property. When I said that one of the reasons I use @property (persistence) is because I don't want to explicitly save my object, I was told that I am "lazy." This is probably true, but I also want to avoid errors that I would forget to save.

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

+7
source share
4 answers

Laziness is a virtue.

I use -> in copyWithZone: to access instance instance variables. (I do not use property attributes here for the same reasons that I do not use them in init or dealloc - they can cause side effects that would be bad in a semi-initialized copy.) I also use self-> in this context, because I like the way of reading:

 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 cannot think of another context in which I use -> for objects.

+6
source

IMO there is not much value when using -> over plain ivar access. This is sometimes useful for debugging purposes if you want to access another ivars object. If this simply distinguishes ivars from local vars, you can also use one of the established naming schemes (without going into details here).

Using properties is a completely different story. IMO in most cases should be preferred to use properties other than simple ivars. Auto save is one of the reasons; a good abstraction is another.

+1
source

For most of the things you do, do not use -> unless you are writing C or C ++ code using Objective-C. It is easier for you, the compiler and memory management, if you create properties and refer to them accordingly.

 @interface MYObject : NSObject { @public // don't use this, please! MYOtherObject *obj; } @property (retain) MYOtherObject *obj; @end @implementation MYObject @synthesize obj; @end // later in your code MYObject *thing = [[MYObject alloc] init]; MYOtherObject *other; // Good other = [thing obj]; other = thing.obj; other = [thing valueForKey:@"obj"]; // I hope my KVC is correct here. :( // Bad, and soon to be deprecated I believe. (all members are @protected unless you use @public) other = thing->obj; 

Why is that bad? You can declare participants in the implementation file as follows: (64-bit and iPhone)

 @interface MYObject : NSObject @property (retain) MYOtherObject *obj; @end @implementation MYObject @synthesize obj = _obj; @end 

Here, calling thing->obj will give nothing but errors, since the element is allocated dynamically at runtime.

Also, as the @Peter Hosey user points out, you can use -> as C ++ Friend in methods like copyWithZone , however, if you don't do such things, then stay away!


[EDIT]

In addition, if you are trying to squeeze performance out of an object, and know what you are doing with this object or member, you can use -> to skip the overhead of the search method. However, even then you can simply program these sections in C or C ++ if performance is a big problem.

Oh, besides, I don't think using -> is thread safe. If your getter / setter uses @synchronize using -> , it bypasses secure devices.

+1
source

self->variable is redundant, as you get the same effect as calling ivar. Typically, the -> operator comes in handy when the item you want to access is ivar in another instance, for example:

 @implementation MyClass - (void) beLikeThisOtherThing:(MyClass *) foo { someVariable = foo->someVariable; } @end 
+1
source

All Articles