user420479
user420479

Reputation:

a synopsis of @property/@synthesize syntax

I agree with the assessment that Apple's docs on @property and@synthesize are very good. Also, I have read the excellent tutorials on stackoverflow.

I would like to ask someone to confirm or correct the following. Given the presence of:

@property UIWindow *itsWindow;      // .h file
@synthesize itsWindow = window_;    // .m file

and considering these statements:

1) self.itsWindow = nil;
2) [self setItsWindow:nil];
3) window_ = nil;
4) itsWindow = nil;

(1) is the message [self itsWindow], whereas (3) and (4) are the actual class param.

If the above is correct, then any of the 4 statements would work in an -init class method.

Upvotes: 2

Views: 362

Answers (1)

Caleb
Caleb

Reputation: 124997

(4) is incorrect. You can't access a property, even within an object, absent an object pointer. So you can say:

  • self.itsWindow
  • [self itsWindow]
  • self.itsWindow = nil
  • aDifferentObject.itsWindow = nil
  • [aDifferentObject setItsWindow:nil]

but you can't just say itsWindow = nil.

Statements 1-3 are all okay, but you generally want to access ivars directly in init and dealloc methods, and use the property accessors everywhere else.

Upvotes: 3

Related Questions