bugfixr
bugfixr

Reputation: 8077

Objective-C: Value of my int not what I expect

I've got a property in a class defined as an int. I retrieve the int value (which is 15) and assign it to a UISlider.value and it is always displaying at the max even though the max is set to 100 - it's because the value being assigned/retrieved is much larger than it should be. I'm sure this is a simple misunderstanding on my part with how object-c and c in general work.

Here's my code:

// device is retrieved from my app delegate NSMutableArray
uiSlider.value = device.nodeLevel;

If I put a breakpoint in at that line and pull up gdb, when I execute this command:

po [device nodeLevel]

It prints "15", which is expected as this is what the int property was set to earlier.

However, when I do this:

print [device nodeLevel]

I end up with the actual value being assigned to the slider's value property... which is "100812800" - it almost seems like that's a memory address or something. In any case, it's not the value I assigned to nodeLevel and consequently isn't the value I want assigned to my slider's value property.

What am I missing?

Just FYI, here's the declaration of my device class:

// Device.H file
@interface Device : NSObject {  
    @private int nodeLevel;
}

@property (readwrite, assign, nonatomic) int nodeLevel;

// Device.m file
@implementation Device

@synthesize nodeLevel; 

- (id)init {
    self.nodeLevel = 0;
    return self;
}

Upvotes: 1

Views: 802

Answers (2)

Yuras
Yuras

Reputation: 13876

Seems it is an address of NSNumber. Check that you initialize nodeLevel properly (e.g. using [NSNumber intValue])

Upvotes: 1

Vertism
Vertism

Reputation: 165

po stands for "print object" so it should be used with an object not a primitive. print will return the actual value of an integer.

I think that nodelevel might have been set to the address of the integer variable i.e.

nodeLevel = &intToUse;

Upvotes: 0

Related Questions