开发者

a synopsis of @property/@synthesize syntax

开发者 https://www.devze.com 2023-03-15 03:03 出处:网络
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 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 thes开发者_运维技巧e 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.


(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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号