gpt4 book ai didi

iphone - 如何声明具有全局类访问权限的 UInt32 变量

转载 作者:行者123 更新时间:2023-11-29 04:46:13 25 4
gpt4 key购买 nike

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

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

我试图在 .h 中这样做

@interface EngineRequests : NSObject {

UInt32 dataVersion;
}

@property (copy) UInt32 dataVersion;

但这不起作用..我在@property等行上收到错误..我是否需要它,或者只在顶部使用UInt32就可以了。

最佳答案

你可以试试

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

@property (assign) UInt32 dataVersion;
@end

@implementation EngineRequests

@synthesize dataVersion;

// methods can access self.dataVersion

@end

但您并不真正需要该属性,除非您想授予/控制外部访问权限。您可以在类接口(interface)中声明 UInt32 dataVersion,然后在实现中引用 dataVersion,而无需 self。 无论哪种方式,@protected 将阻止外部类直接访问 dataVersion

您读过Objective-C Properties吗? ?

初始化

您的EngineRequestsNSObject的子类。因此,您可以(通常应该)重写 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;

关于iphone - 如何声明具有全局类访问权限的 UInt32 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9612130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com