views:

219

answers:

3

I have a very basic question regarding properties in Objective-C.

I can only access object's properties via dot notation (Obj.MyProp) if I @synthesize myProp. Is that correct?

Would it be true to say that if I use my own setter method, I will no longer be able to refer to property in dot notation?

Basically I am looking for C# type of functionality where I can write my own custom getter/setter and yet provide an additional code which I need to execute when the property is set.

+3  A: 

This is not correct. You can still use dot-notation even if you write custom getters or setters provided of course that your getters and setters maintain the correct method naming for the property.

rein
+1  A: 

From the docs:

@synthesize

You use the @synthesize keyword to tell the compiler that it should synthesize the setter and/or getter methods for the property if you do not supply them within the @implementation block.

It only synthesizes if you haven't already written them. If you've written them, they don't get synthesized.

Terry Wilcox
as long as your getters and setters are KVC compliant for getters and setters, dot notation should work.
pxl
What's that got to do with @synthesize?
Terry Wilcox
What did this get voted down for? Acurracy?
Terry Wilcox
+4  A: 

@property creates automatic message declarations, just like writing

(int)thing;
(void)setThing:(int)value;

@synthesize automatically creates the implementations, i.e.

(int)thing {
    return thing;
}
(void)setThing:(int)value {
    thing = value;
}

If you give a definition yourself, it overrides the @synthesized version. So as long as you name a method correctly, it will work, with or without @synthesize in there.

Dot notation works with either synthesized or custom method implementations.

Chris Burt-Brown
A thing to note here is that the actual name setThing is important. If your setter has a different name, you can override that with the setter attribute of the @property declaration.
nall