开发者

Question about @property and @synthesize

开发者 https://www.devze.com 2023-03-02 15:40 出处:网络
Usually we have codes like these @interface TestAppDelegate : NSObject <UIApplicationDelegate> {

Usually we have codes like these

@interface TestAppDelegate : NSObject <UIApplicationDelegate> {

}
@property (nonatomic, retain) IBOutlet UIWindow *window;

@synthesize window;

And also second version

@interface TestAppDelegate开发者_如何学JAVA : NSObject <UIApplicationDelegate> {
      UIWindow *_window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;

@synthesize window = _window;

Quetions are

  • what is the big difference ? Which better ? and Why ?
  • For first version, Why what is the default attribute member of window ,which is _window in second version. Does that KVC or KVO work inside ?


in the default instance it is the same as

@interface TestAppDelegate : NSObject <UIApplicationDelegate> {
      UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;

@synthesize window = window;

which would actually not need an Assignment on the synthesize. and would probably generate an error.

This is the normal way that I do it.

@interface TestAppDelegate : NSObject <UIApplicationDelegate> {
      UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;

@synthesize window;

I explicitly declare the Instance Variable

UIWindow *window;

so that I know it is there, (they are generally all right next to each other.)

and I separate them by the ones I retain and the ones I dont. So that I know to release them in my dealloc method

0

精彩评论

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

关注公众号