网上有这样一个描述:
Xcode编译错误:Synthesized property 'xxxXXX' must either be named the same as a compatible ivar or mus
- // 2011.07.21
- // Xcode 4.0.2
- // 64-bit
- @interface IvarNameTest : NSObject {
- @private
- }
- @property(nonatomic) NSNumber *number;
- @property(nonatomic) float f;
- - (void)printValue;
- @end
- #import "IvarNameTest.h"
- @implementation IvarNameTest
- @synthesize number = anyIdentifier;
- @synthesize f = anyIdentifier2;
- - (void)printValue
- {
- anyIdentifier = [NSNumber numberWithDouble:77.77];
- anyIdentifier2 = 7.7f;
- NSLog(@"%@, %f", anyIdentifier, anyIdentifier2);
- }
- @end
说明:
在 64-bit 平台下编译,在 @interface 块中如果没有定义 instance variable,给出了 @property 声明,同时在 @implementation 块中给出了 @synthesize。
结论:
1)如果是 @synthesize name; 形式,则编译器自动创建的 instance variable 名字就是 name,也就是 @property 声明中的名字;
2)如果是 @synthesize name = XXXX; 形式,则编译器自动创建的 instance variable 的名字就是 XXXX。
我觉得系统就是自动生成了一个用_为前缀的变量名.
在设置为属性的时候,可以明确表示使用这个 "_xx"的变量,如果不写的话,就标识,私有变量也是本身的名字,就是不带_这个前缀了.