ios内存管理笔记(二)

1)实现部分:

复制代码
  1. @synthesize window=_window;
  2. @synthesize viewController=_viewController;


通常看到的都没有包含=部分,@synthesize window=_window; 怎么理解?这里的 _window 和 _viewController 是什么变量?
2).h 文件中在类中没有定义 window 和 viewController 实例变量,怎么能进行 @perproty 声明呢?

复制代码
  1. // .h
  2. #import <UIKit/UIKit.h> 
  3. @class HelloWorldMailViewController;
  4. @interface HelloWorldMailAppDelegate : NSObject <UIApplicationDelegate> {
  5. @property (nonatomic, retain) IBOutlet UIWindow *window;
  6. @property (nonatomic, retain) IBOutlet HelloWorldMailViewController *viewController; 
  7. @end



复制代码
  1. // .m
  2. // ...
  3. @implementation HelloWorldMailAppDelegate 
  4. @synthesize window=_window;
  5. @synthesize viewController=_viewController; 
  6. // ... 
  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

@synthesize window=_window; 意思是说,window 属性为 _window 实例变量合成访问器方法。
也就是说,window属性生成存取方法是setWindow,这个setWindow方法就是_window变量的存取方法,它操作的就是_window这个变量。
下面是一个常见的例子
@interface MyClass:NSObject{
  MyObjecct *_myObject;
}
@property(nonamtic, retain) MyObjecct *myObject;
@end

@implementatin MyClass
@synthesize myObject=_myObject;
这 个类中声明了一个变量_myObject,又声明了一个属性叫myObject,然后用@synthesize生成了属性myObject的存取方法,这 个存取方法的名字应该是:setmyObject和getmyObject。@synthesize myObject=_myObject的含义就是属性myObject的存取方法是做用于_myObject这个变量的。
这种用法在Apple的Sample Code中很常见,

原文地址:https://www.cnblogs.com/lisa090818/p/3214420.html