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

网上有这样一个描述:

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

[plain]  view plain copy print ?
  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 plain copy print ?
  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"的变量,如果不写的话,就标识,私有变量也是本身的名字,就是不带_这个前缀了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值