Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何使用全局类访问声明UInt32变量_Iphone_Ios_Uint32 - Fatal编程技术网

Iphone 如何使用全局类访问声明UInt32变量

Iphone 如何使用全局类访问声明UInt32变量,iphone,ios,uint32,Iphone,Ios,Uint32,我试图声明一个UInt32变量,该变量可以被类中的任何方法访问 所以它对类方法是全局的,但对任何其他类都不是全局的 我正试着在h.h @interface EngineRequests : NSObject { UInt32 dataVersion; } @property (copy) UInt32 dataVersion; 但这不起作用。。我在@property等行上得到一个错误。。我是否需要它,或者只在顶部使用UInt32就可以了。您可以试试 @interface Engin

我试图声明一个UInt32变量,该变量可以被类中的任何方法访问

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

我正试着在h.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
但您并不真正需要该属性,除非您希望授予/控制外部访问权。您可以在类接口中声明
UInt32 dataVersion
,然后在实现中引用
dataVersion
,而不使用
self。
无论哪种方式,
@protected
都将阻止外部类直接访问
dataVersion

你读过这本书吗

初始化

您的
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)版本

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

@接口引擎请求:NSObject{

UInt32 dataVersion;
}


没什么了。

这工作得很好,比如说,如果我有一个方法通过一个参数初始化UInt32变量,我该怎么做?我已经尝试过这个-(void)initializepacketvariables:(UInt32)版本{//…但是这会导致错误..添加了一个更新。您在评论中描述的方法也可以很好地工作。
init
内容是Cocoa中的最佳实践。好的,很酷..我将尝试解决所有问题。感谢您的帮助刚刚意识到它不起作用的原因..我在哪里贴花了我的方法,我没有更改为just(UInt32)我仍然有(UInt32*)…在我学会这个把戏之前有多少次。哈哈,是的,我想你想要真正的价值。谢谢你的道具!