0

我正在尝试声明一个 UInt32 变量,该变量可以由类中的任何方法访问。

所以它对类方法是全局的,而不是对任何其他类...

我正在尝试在 .h 中这样做

@interface EngineRequests : NSObject {

    UInt32 dataVersion;
}

@property (copy) UInt32 dataVersion;

但这不起作用..我在@property等行上遇到错误..我什至需要那个还是只使用顶部的UInt32就可以了。

4

2 回答 2

1

你可以试试

@interface EngineRequests : NSObject {
@protected
   UInt32 dataVersion;
}

@property (assign) UInt32 dataVersion;
@end

@implementation EngineRequests

@synthesize dataVersion;

// methods can access self.dataVersion

@end

但是您并不真正需要该属性,除非您想授予/控制外部访问权限。您可以只UInt32 dataVersion在类接口中声明,然后dataVersion在没有self.任何方式的实现中引用,@protected将阻止外部类dataVersion直接访问。

你读过Objective-C 属性吗?

初始化

EngineRequests是 的子类NSObject。因此,您可以(通常应该)覆盖NSObject-(id)init方法,如下所示:

-(id)init {
   self = [super init];
   if (self != nil) {
      self.dataVersion = 8675309; // omit 'self.' if you have no '@property'
   }
   return self;
}

或创建自己的-(id)initWithVersion:(UInt32)version;.

于 2012-03-08T02:32:45.920 回答
0

您需要仅在接口内声明变量以使其对所有类方法可见。使用 @property.... 创建 getter-setter 将使其成为类变量,并且在类外可见。你必须这样做。

@interface EngineRequests:NSObject {

UInt32 dataVersion;

}

而已。

于 2012-03-08T02:35:45.840 回答