@synthesize window=_window;


@synthesize viewController=_viewController;

 

通常看到的都没有包含=部分,@synthesize window=_window; 怎么理解?这里的 _window 和 _viewController 是什么变量?

.h 文件中在类中没有定义 window 和 viewController 实例变量,怎么能进行 @perproty 声明呢?


   
  1. #import <UIKit/UIKit.h>
  2. @class  ViewController;@interface  AppDelegate : NSObject 
  3. <UIApplicationDelegate>  
  4.  
  5. @property (nonatomic, retain) IBOutlet UIWindow *window; 
  6. @property (nonatomic, retain) IBOutlet ViewController *viewController; 
  7.  @end  

 

在 32-bit 时,如果类的 @interface 部分没有进行 ivar 声明,但有 @property 声明,在类的 @implementation 部分有响应的 @synthesize,则会得到类似下面的编译错误:
Synthesized property 'xX' must either be named the same as a compatible ivar or must explicitly name an ivar
在 64-bit时,运行时系统会自动给类添加 ivar,添加的 ivar 以一个下划线"_"做前缀。

上面声明部分的 @synthesize window=_window; 意思是说,window 属性为 _window 实例变量合成访问器方法。

参考http://www.cocoabuilder.com/archive/cocoa/198573-property-problem.html 

如果不明确的指明私有变量的名称的话,系统就会认为你的私有变量名和属性名是一样的!