How are my variables private if i use @property/synthesize?


#1

Hello

I have a quick question. On page 103 the book states that in object oriented programming we try our best to keep the instance variables private. We do not want the user to be accessing them directly. Hence we use methods (which i think of as functions inside the object). That makes sense since we do not have direct contact with the variables. But why?

Also when we use @property/synthesize method of writing, are we not using the variable names to access the setter and getter methods? Like in the example, our instance variables are called heightInMeters and weightInKilos. Using the synthesize “feature” i no longer have the option to have my own function name which returns heightInMeters. Instead, i have to know what the variable is like heightInMeters and then when i call it, the compiler creates a function which returns the variable value. I dont know if i explained it properly but i dont see the point in that case since the variable is not “masked” by the function anymore.

Your help is greatly appreciated :slight_smile:


#2

If you declare and synthesise properties without providing accessor methods (getters and setters), accessing the properties (aside from some side effects) is akin to accessing the corresponding instance variables directly.

However, by providing your own accessors, you can take full control over data access and implement your own policy.

//  main.m

#import <Foundation/Foundation.h>

@interface Foo: NSObject
@property int fooness;
@end

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        
        Foo *foo = [[Foo alloc] init];
        foo.fooness = -1;
    }
    return 0;
}

@implementation Foo

@synthesize fooness;

- (int)fooness
{
    return fooness;
}

- (void)setFooness:(int)v
{
    assert (v != -1);
    fooness = v;
}
@end

#3

Thanks for the quick response :slight_smile:

I understand a little bit now. However whats the point of declaring the instance methods with @property and @synthesize then? You had to write your own implementation anyways with your validation scheme for the setter anyways. How does that save you a line of code? Maybe you didnt have to write declare the following in the .h file


-(void)setFooness:(int)v

You probably didnt even need to write the implementation for getting fooness either correct considering synthesize would take care of it?

Is that correct understanding?

I guess what im trying to clarify is why choose the @property/synthesize way of doing it when you end up having to define your own validation scheme anyways for accessors in your .m file. Is it just because it saves you the declarations in the .h file?

I appreciate you helping me understand this :slight_smile:


#4

[quote]You probably didnt even need to write the implementation for getting fooness either correct considering synthesize would take care of it?

Is that correct understanding?
[/quote]
That’s correct, but getters can do extra work in addition to just returning a value.

For example:

- (int)fooness
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Alert - Fooness about to be accessed" object:self userInfo:nil];
    return fooness;
}

Just hang in there; you will get to appreciate properties as you become more experienced with the Objective-C Programming language and the associated run time system.

For example, synthesised property setters do extra work behind the scenes.

Take a look at this post: viewtopic.php?f=135&t=5265&p=13690#p13690

I think the reason for introducing properties was to eliminate code that would have to be written manually for simple tasks of reading and modifying instance data (and for sending notifications).


#5

Ahh got it! Thank you :slight_smile: Good to know such helpful people are on the forums.