Search code examples
iphoneiosuint32

how to declare UInt32 variable with global class access


I am trying to declare a UInt32 variable that can be accessed by any method in the class..

so its global to the classes methods but not to any other class...

I am trying to do it like this in the .h

@interface EngineRequests : NSObject {

    UInt32 dataVersion;
}

@property (copy) UInt32 dataVersion;

but thats not working.. I'm getting an error on the line @property etc.. do I even need that or is it fine to just use the UInt32 at the top.


Solution

  • You could try

    @interface EngineRequests : NSObject {
    @protected
       UInt32 dataVersion;
    }
    
    @property (assign) UInt32 dataVersion;
    @end
    
    @implementation EngineRequests
    
    @synthesize dataVersion;
    
    // methods can access self.dataVersion
    
    @end
    

    But you don't really need the property, unless you want to grant/control outside access. You could just declare UInt32 dataVersion in the class interface and then reference dataVersion in the implementation without self. Either way, @protected will prevent outside classes from accessing dataVersion directly.

    Have you read up on Objective-C Properties?

    Initialization

    Your EngineRequests is a subclass of NSObject. As such, you can (usually should) override NSObject's -(id)init method, like such:

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

    Or create your own -(id)initWithVersion:(UInt32)version;.