Take the 2-minute tour ×

Let's say that I declare property in following way:

@property(nonatomic, strong, getter = isWorking) BOOL working;

Then instead of having the property to be synthesized I write the getter myself (and add some custom logic to it).

What will happen if I access the property in following way:

BOOL work = self.working;

Is the getter (and my custom logic there) still called or is it called only when I access the property using getter explicitly (BOOL work = self.isWorking;) ?

share|improve this question
Similar: stackoverflow.com/questions/9220028/…. –  Martin R Aug 17 '13 at 18:27
add comment

1 Answer

up vote 3 down vote accepted

Oops. Just tried it. Apparently i use dot notation too much, and didn't realize just how much it was doing. :P

#import "NSObject.h"
#include <stdio.h>

@interface Test : NSObject
@property (getter=myStuff) int stuff;

@implementation Test
-(int)myStuff { return 42; }
-(void)setStuff:(int)value { /* don't care */ }

int main() {
    @autoreleasepool {
        Test* test = [[Test alloc] init];

        /* All these work... */
        printf("test.stuff == %d\n", test.stuff);
        printf("[test myStuff] == %d\n", [test myStuff]);
        printf("test.myStuff == %d\n", test.myStuff);

        /* but here, there's an exception */
        printf("[test stuff] == %d\n", [test stuff]);

        return 0;

When i compile this (using clang in Linux), there are two warnings about the oddness of a missing -(int)stuff. And the output looks like

chao@chao-VirtualBox:~/code/objc$ ./a.out
test.stuff == 42
[test myStuff] == 42
test.myStuff == 42
: Uncaught exception NSInvalidArgumentException, reason: -[Test stuff]: unrecognized selector sent to instance 0x2367f38

So, umm, yeah. Disregard half of the stuff below. :P

self.working is just syntactic sugar for [self working] (or [self setWorking:value] if you're assigning to it). Either one will do the same thing: return the value of [self isWorking], because that's the getter you defined.

If you want to avoid the getter, try _working or self->_working (or whatever you named the ivar). Otherwise, self.working, [self working], and [self isWorking] (and even self.isWorking if you're feeling brave) should all give you the same result.

share|improve this answer
I do not want to avoid it - I want it to be always called. I know that self.working is just syntactic sugar for [self working] but my getter is not named working but isWorking. Please read my question carefully once again. –  drasto Aug 17 '13 at 18:00
Umm...of course it's the same thing. You've explicitly arranged that working's getter is isWorking. So that's what'll be called when you try to get the property. I misinterpreted it because that seemed a given. :P –  cHao Aug 17 '13 at 18:15
This is slightly misleading; that's probably why it was downvoted, @jlehr. self.propertyName; is syntactic sugar for [self propertyGetterName];, whatever the getter name is. If the getter doesn't have the same name as the property, then saying the sugar is for [self propertyName]; isn't correct. In fact, the method - (propertytype) propertyName; isn't created in that case, and you wouldn't be able to call it; the last sentence is completely wrong in that respect. –  Josh Caswell Aug 17 '13 at 19:02
Josh Caswell is correct. There is no -working getter method here. [self working] won't work. It won't even compile. There is a property named "working", so that self.working compiles, but the getter is -isWorking and that's what it calls. See my answer to a similar question to see why self.isWorking also works but is sort of wrong. –  Ken Thomases Aug 17 '13 at 19:09
@KenThomases: Oh...damn...yeah, you're right. I thought for sure that ObjC was doing some stuff to respond to both messages, cause i'd semi-accidentally used self.methodName rather than [self methodName] and everything worked. Turns out that dot means more than i thought it did. :P Edited with the results of my experiment. –  cHao Aug 18 '13 at 3:49
show 7 more comments

Your Answer


