views:

36

answers:

1

Hi,

I'm new to Objective C, but have extensive C++ experience.

I have a member variable called bOn, which I have declared as a readonly property. I then synthesize it.

However, the compiler won't let me read it, saying "Instance Variable 'bOn' is declared protected". I would understand this error if I had not synthesized.

Here are my snippets:

@interface Button : NSObject

{

    . . .
    BOOL bOn;
}

@property (nonatomic, readonly) BOOL bOn;

And where I use it:

 -(void) updateForButtonLeft:(Button *)butLeft Right:(Button *)butRight

{

    BOOL bLeft = butLeft->bOn;
    . . .

So what else am I forgetting to do?

Thanks,

Dave.

+4  A: 

butLeft->bOn; is direct instance variable access and under all but very rare circumstances, is a really bad idea.

What you're looking for is:

BOOL bLeft = butLeft.b0n;

Or

BOOL bLeft = [butLeft b0n];
Dave DeLong
That's great, cheers!I think the C++ in me was taking over.So dot-notation can be used even if it's a pointer to the object (as I think it always will be in ObjC?)Thanks
Dave
@Dave in Objective-C, `myObject.foo` is nothing more than syntactic sugar for `[myObject foo]` or `[myObject setFoo:...]`, depending on which side of the `=` it's on.
Dave DeLong