tags:

views:

38

answers:

3

I want to access primitive data types like int, Bool in Objective C. I have created one class in which I declared an integer varriable. Now in other class i want to access this data and also set the value of it.

As in Objective C setter methods are applicable only for objects and I can't convert int to object.

So how would I be able to access the data ?

Please suggest some approach.

+3  A: 

You can use getters and setters with primitives.

Just use @synthesize, or create your own methods:

- (int)primitiveIvar;
- (void)setPrimitiveIvar:(int)_ivar;
chpwn
A: 
willc2
A: 

As in Objective C setter methods are applicable only for objects and I can't convert int to object.

Where did you get that idea. You can have Objective-C properties that are primitive types using either properties or normal accessors.

// in the .h file
// intIVar and otherIntIvar are int instance variables

@property (assign) int myIntIVar;
//         ^^^^^^ stops the runtime from sending retain or copy to synth'd ivars
-(int) myOtherIntIVar;
-(void) setMyOtherIntIVar;

// in the .m file

@synthesize myIntIVar = intIVar;

-(int) myOtherIntIVar
{
    return otherIntIVar;
}

-(void) setMyOtherIntIvar: (int) newValue
{
    otherIntIvar = newValue;
}
JeremyP