1

New to Objective-C, and i am basically from c++ background. I am learning now objective-c and would like to get confirmation of what i understood is write or wrong?. Kindly advise.

I have the following class:

@interface Test: NSObject
{
 int instance1; 
}
@property int instance1;
 - (void)  sayHello;
@end

Class 'Test' has a instance variable instance1. If the member function ie: sayHello wants to access the variable, it has to happen through getter/setter functions. So, There are two ways to get it :

  1. User can define.
  2. We can get the help from the compiler?. How?. declare the same variable as a property, and synthesize it, the the compiler gets the code of getter/setter for us for that particular variable.

So, Untimately, getter/setter is the only way to access the variable in the method implementation, ie. both self.instance1 = 100; and instance1 = 100 need getter/setter.

Having missed both 1. and 2., there is no way to access the instance1 variable.

Also, instance1 is a pubic variable can can be accessed outside of the class with object instance.

Test *t = [[ Test alloc] init];
t.instance1 = 200;

Questions:

  1. Is there any way to make instance1 is "private", so that I can not access the instance variable outside the class?
  2. Is there anything wrong in my understanding?
DrummerB
  • 39,814
  • 12
  • 105
  • 142
Whoami
  • 13,930
  • 19
  • 84
  • 140

2 Answers2

1

If the member function ie: sayHello wants to access the variable, it has to happen through getter/setter functions.

It doesn't have to. You can access ivars directly, without using accessor methods:

- (void)sayHello {
   instance1 = 123;
}

You can define private ivars by declaring them in the implementation file, not the header:

@implementation Test {
   int privateVar;
}
// ... additional implementation, methods etc.
@end

Note, that since Xcode 4.4 you don't have to declare your ivars anymore. You simply declare a property. The ivar and the accessor methods will be synthessized automatically.

For more details, I recommend reading my answer to this question: Declaration of variables

Community
  • 1
  • 1
DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • Thanks for the comment, Yup, i see i could able to access instance1 withouth getter/setter. So, if i understand correctly the only difference between self.instance1 = 100, and instance1 = 100, is the first one uses accessor method, where as second one directly assigns. In that case why two different set of usage? – Whoami Sep 28 '12 at 14:28
  • Correct. `self.instance1 = 100` uses the accessor methods, while `instance1` directly sets the ivar. There are a couple of differences. If you override the setter method, it will only be executed if you use the dot syntax or call the setter using `[test setInstace1:100];`. You might want to override a setter to execute additional code in response to changes of the variable. Directly setting the ivar (`instance1 = 100`) won't execute the setter and the additional code. – DrummerB Sep 28 '12 at 14:33
  • 1
    An other reason is [KVC](http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html). Any class can observe changes to a KVO compliant property. So, if the value changes, it will be notified and can respond to that change. However, KVO only works if the variable is changed via accessor methods. Directly changing the variable won't trigger any KVO notifications. – DrummerB Sep 28 '12 at 14:34
  • DrummerB, That's the use case i was looking. Nice explanation. Accepting. :). – Whoami Sep 28 '12 at 14:58
1

ion SomeDelegate.h

@interface SomeDelegate : NSWindowController {
@private
    int fLanguage;
    int fDBID;

    bool fEndEditingIsReturn;
@public
    int fIsMyLastMSG;  
}

@property int language;

In SomeDelegate.mm

@implementation SomeDelegate

@synthesize language=fLanguage;

In my example you get private and public variables, private variable fLanguage has a property for synthesize accessor methods.

Shebuka
  • 3,148
  • 1
  • 26
  • 43