2

If im declaring a class with property for example:

@property float radius, diameter;

So any object that will be created in this class or inherits from it, will have its own instance of radius and diameter right?

And this is also mean that they have their own setter and getter too right?

What i'm trying to fully understand is how do I deal with variable access in 2 ways:

1.When I want to make sure the user is not messing with the original value.

2.When I want to limit the users for instance variables he can get access and ones that he can not.

tnx

MNY
  • 1,506
  • 5
  • 21
  • 34

1 Answers1

0

As to the first part, yes, any subclass will inherit it's superclasses properties and methods as long as they are public. Just remember to synthesise them to get the getter and setter.

So, if you want to have a public property that can't be modified:

// This returns a copy - whatever the receiver does with it doesn't affect the original
@property (nonatomic, copy) BOOL someVariable;

// This declares the property as only having a getter - no setter
@property (nonatomic, readonly) NSArray *someArray;

Any properties declared in your header file are considered public, unless otherwise specified. If you want to have private properties, ivars or methods, declare them in your implementation file in the class continuation category:

// In your .m *above* the @implementation MyClass
@interface MyClass()
@property (nonatomic, strong) NSArray *myPrivateModel;
@end

Stamford do a great lecture series on iTunesU which I'd really recommend for learning objectiveC

Hope that helps

0xWood
  • 1,326
  • 12
  • 15
  • 1
    Note also that you can provide your own getter/setter functions so you can control not only who can modify your property, but how... for example, to restrict modifications to be within a valid range. – mah Feb 14 '13 at 01:11
  • @ChristopherWooden thank you buddy! im learning currently learning Objective C with "Programming in Objective C" of Stephen G. Kochan, I actually really enjoy it so far. I though maybe next to do the stanford course, since I will have stronger obj c. But its weird that in the book he is not using `(nonatomic, readonly)` or `(nonatomic, copy)`. Just `@property float radius, diameter; `... – MNY Feb 14 '13 at 02:04
  • If you're synthesizing properties, the code that is generated is affected by whether they're atomic or not. A great SO thread on the subject can be found [here](http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties). – 0xWood Feb 14 '13 at 02:24