0

XCode just corrected my use of the . operator to the -> operator.

I have searched around to find a definition or documentation for the -> operator but could find any.

I am trying to emulate a Java-style of instance variable. Specifically I am initializing an instance with a provided configuration object which I want to keep around for subsequent use after the method initializeService has executed.

A good answer to this question would provide a comparison with Java instance variable declaration and subsequent assignment via an instance method.

Code before XCode correction:

#import "MY_Service.h"
#import "MY_Configuration.h"

@implementation MY_Service {

    MY_Configuration *configuration;

}

-(void)initializeService:(MY_Configuration *)configuration
{
    self.configuration = configuration;

}


@end

Code after XCode correction:

#import "MY_Service.h"
#import "MY_Configuration.h"

@implementation MY_Service {

    MY_Configuration *configuration;

}

-(void)initializeService:(MY_Configuration *)configuration
{
    self->configuration = configuration;

}


@end
Guido Anselmi
  • 3,872
  • 8
  • 35
  • 58
  • change `configuration` to be a property instead of an ivar. – CrimsonChris May 27 '14 at 18:26
  • Note that you don't have to use self at all to reference ivars. – CrimsonChris May 27 '14 at 18:29
  • 1
    Objective-C instance variables are ordinarily prefixed with an underscore. If you change your declaration to follow this convention you can rewrite your assignment as `_configuration = configuration`. – jlehr May 27 '14 at 18:36
  • @jlehr - Thanks. I made your change. My assumption is that I have properly scoped the configuration object as an instance variable and not as a property. Below an answer suggests that I make it a property. In Java it would be an instance variable that is kept private after initializeService is called. (i.e. there would be no getter or setter). Have I made the right choice? – Guido Anselmi May 27 '14 at 18:40
  • 1
    @GuidoAnselmi There are no absolutes here, but property declarations do have certain advantages, for example allowing you to specify additional things such as `copy` semantics for memory management, atomicity, etc. But your code correctly declares a private instance variable that would work as you expect. – jlehr May 27 '14 at 18:48
  • @jlehr - Thanks again. You've answered my question. If you copy & paste your comments as an answer then all the answer booty is yours. – Guido Anselmi May 27 '14 at 18:55

3 Answers3

4

The -> means the same thing in Objective-C that it does in C. That is:

a->b 

is equivalent to

(*a).b
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 2
    I think it is not completely the same when the "modern" Objective-C runtime is used. The ivars do not have fixed offsets to the beginning of the struct anymore to solve the fragile base class problem. Compare http://www.sealiesoftware.com/blog/archive/2009/01/27/objc_explain_Non-fragile_ivars.html. – Martin R May 27 '14 at 18:34
  • 1
    @MartinR - A quick (hence incomplete) check shows `iVar`, `self->iVar` and `(*self).iVar` all produce the same code. I.e. the implementation detail that both `.` and `->` are no longer using fixed offsets is correctly handled by the compiler as it should be and the equivalence still holds. Indeed I don't think fixed offsets were ever part of the C Standard definition of member access. – CRD May 27 '14 at 21:43
  • @CRD: My point (perhaps badly expressed) is that `->` does not do exactly the same thing for C structs vs Objective-C classes. I am quite sure that members in C structs have a fixed offset that is determined at compile time (and can be determined with the `offsetof()` macro from stddef.h). For Objective-C objects, `obj->_ivar` does a double indirection (as explained in Greg Parker's blog). – Martin R May 28 '14 at 07:07
  • 1
    @MartinR - Understood. Similarly my point is that the *meaning* is the same, member (field or ivar) access and the equivalence holds, just as Carl stated. That the *implementation* differs doesnt change that. Have a nice day. – CRD May 28 '14 at 09:06
2

-> is used to access instance variables, the same way it's used to access member variables of a pointer in C or C++.

In the code you provided, you declared configuration as an instance variable. If you wanted to use the dot operator, you should have declared it as a property, which is what you really should have done from the beginning.

Mohannad A. Hassan
  • 1,650
  • 1
  • 13
  • 24
  • A Hassan: Why should it be a property if I don't want to access it from outside the instance? Thanks. – Guido Anselmi May 27 '14 at 18:36
  • 1
    I think of it as a habit. Declaring a property doesn't take much space than declaring an ivar, and in modern Objective-C it's automatically synthesized. The cost of a call to them isn't costly, unless you're doing some heavy optimization. It gives you the chance to add behaviour, like strong or weak referencing and atomicity. **The point is**, it's not tedious to write and it gives you capabilities. – Mohannad A. Hassan May 27 '14 at 18:48
2

Objective-C instance variables are ordinarily prefixed with an underscore. If you change your declaration to follow this convention you can rewrite your assignment as

_configuration = configuration;

With respect to whether it would be better to declare a property or an instance variable, there are no absolutes here, but property declarations do have certain advantages. Some examples would be allowing you to specify additional things such as copy semantics for memory management, atomicity, etc. But your code correctly declares a private instance variable that would work as you expect.

jlehr
  • 15,557
  • 5
  • 43
  • 45