0

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.

C.Johns
  • 10,185
  • 20
  • 102
  • 156

2 Answers2

1

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;.

QED
  • 9,803
  • 7
  • 50
  • 87
  • That worked perfectly, say if i had a method that was going to initalize that UInt32 var via a parameter, how would I do that? I have tried this **- (void) initalizePacketVariables:(UInt32)version {//...** However thats causing errors.. – C.Johns Mar 08 '12 at 02:37
  • Added an update. The method you describe in your comment would work fine too. The `init` stuff is best practice in Cocoa. – QED Mar 08 '12 at 02:44
  • okay cool.. I will try and figure this all out then. thanks for the help – C.Johns Mar 08 '12 at 02:48
  • just realized why it wasnt working.. where i was decalring my method I had not changed to just (UInt32) i still had (UInt32 *).. how many times before I lear that trick. lol – C.Johns Mar 08 '12 at 02:50
  • Oops, yes, I guess you want the real value. Thanks for the props! – QED Mar 08 '12 at 02:53
0

You need to declare the variable only inside interface to make it visible to all classes methods. Creating the getter-setter using @property.... will make it class variable and it will be visible outside the class. you have to do just this.

@interface EngineRequests : NSObject {

UInt32 dataVersion;

}

nothing more.

ArunGJ
  • 2,685
  • 21
  • 27