xcode4以后关于私有变量的定义问题

来源:互联网 发布:手机淘宝直通车怎么弄 编辑:程序博客网 时间:2024/05/19 13:56

网上有这样一个描述:

Xcode编译错误:Synthesized property 'xxxXXX' must either be named the same as a compatible ivar or mus

[plain] view plaincopyprint?
  1. // 2011.07.21   
  2. // Xcode 4.0.2  
  3. // 64-bit  
  4.       
  5. @interface IvarNameTest : NSObject {  
  6. @private  
  7. }  
  8.      
  9. @property(nonatomic) NSNumber *number;  
  10. @property(nonatomic) float f;  
  11.      
  12. - (void)printValue;  
  13. @end  
[plain] view plaincopyprint?
  1. #import "IvarNameTest.h"  
  2.      
  3. @implementation IvarNameTest  
  4.     
  5. @synthesize number = anyIdentifier;  
  6. @synthesize f = anyIdentifier2;  
  7.      
  8. - (void)printValue  
  9. {  
  10.     anyIdentifier = [NSNumber numberWithDouble:77.77];  
  11.     anyIdentifier2 = 7.7f;  
  12.     NSLog(@"%@, %f", anyIdentifier, anyIdentifier2);  
  13. }  
  14. @end  

说明:

在 64-bit 平台下编译,在 @interface 块中如果没有定义 instance variable,给出了 @property 声明,同时在 @implementation 块中给出了 @synthesize。

结论:

1)如果是 @synthesize name; 形式,则编译器自动创建的 instance variable 名字就是 name,也就是 @property 声明中的名字;

2)如果是 @synthesize name = XXXX; 形式,则编译器自动创建的 instance variable 的名字就是 XXXX。


我觉得系统就是自动生成了一个用_为前缀的变量名.

在设置为属性的时候,可以明确表示使用这个 "_xx"的变量,如果不写的话,就标识,私有变量也是本身的名字,就是不带_这个前缀了.

原创粉丝点击