您的位置:首页 > 其它

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

2012-07-13 14:06 337 查看
网上有这样一个描述:

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

[plain] view
plaincopyprint?

// 2011.07.21

// Xcode 4.0.2

// 64-bit

@interface IvarNameTest : NSObject {

@private

}

@property(nonatomic) NSNumber *number;

@property(nonatomic) float f;

- (void)printValue;

@end

[plain] view
plaincopyprint?

#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[b] variable,给出了 @property 声明,同时在 @implementation 块中给出了 @synthesize。[/b]

结论:

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

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

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

在设置为属性的时候,可以明确表示使用这个 "_xx"的变量,如果不写的话,就标识,私有变量也是本身的名字,就是不带_这个前缀了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: