开发者

Using int datatype in objective-c

开发者 https://www.devze.com 2023-01-23 10:09 出处:网络
First, I\'m very new with objective-c and memory management, pointers, etc. No doubt my problem lies in a simple point I\'m missing.

First, I'm very new with objective-c and memory management, pointers, etc. No doubt my problem lies in a simple point I'm missing.

I've got a class which contains a property for an integer:

// 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;
}

I create an NSMutableArray of many Device objects, assigning the node Id:

-(NSMutableArray *)getDevices {

...

NSMutableArray *devices = [[NSMutableArray alloc] initWithCapacity:[rDevices count]];

for (NSDictionary *d in rDevices) {
     Device *newDevice = [[Device alloc] init] autorelease];
     newDevice.nodeLevel = d.nodeLevel;

     [devices addObject: newDevice];
}

return [devices autorelease];

}

My devices array is stored on the main app delegate where I've got a property assigned to hold it:

@property (nonatomic, retain) NSMutableArray *devices;

Now here's where my problem is manifest. I'm using a tableView in another controller class to access my app delegate, pull a device from its array then set values with the int, but 'strange' things happen:

EDIT: Min/Max values for the slider are set in another part of the code to 0 and 100 respectively.

// In method cellForRowAtIndex

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Device *d = (Device *)[[appDelegate.devices objectAtIndex:indexPath.row]];

// cell is a custom cell with a UISlider objec开发者_StackOverflow社区t       
cell.sliderLevel.value = [d nodeLevel];

When I assign value to the device's nodeLevel, the slider is always maxed out, even if nodeLevel only equals 1 or 2.

If I do this instead, the slider is at the correct position, but I'll eventually get a "EXC_BAD_ACCESS" signal when scrolling up and down through my tableView:

// cell is a custom cell with a UISlider object   
cell.sliderLevel.value = [[d nodeLevel] intValue];

I suspect that I must be assigning the value to a memory location in the first instance? In the second case it works, but I assume that my BAD_ACCESS is a result of the nodeLevel becoming "released" or something? One final note, I've also got an NSString object associated with the Device class. I access the string and assign it to a label in my cell and it never causes me problems.

Thanks in advance for taking a look.


What type is returned by the nodeLevel property in this line: "newDevice.nodeLevel = d.nodeLevel;"? The nodeLevel property in Device is an int, so you need to ensure that d.nodeLevel is returning an int, and not an NSNumber object.

If d.nodeLevel is returning an NSNumber, that would explain why calling intValue on it gets you a reasonable value, and you get a huge number if you don't call intValue on it (the huge value would be the pointer value for the NSNumber object). It would also explain why you get an EXC_BAD_ACCESS crash later on, because your NSNumber object isn't being retained.

You should probably just change this line:

newDevice.nodeLevel = d.nodeLevel;

to

newDevice.nodeLevel = [d.nodeLevel intValue];

and don't call intValue on it later on, so you would change this:

cell.sliderLevel.value = [[d nodeLevel] intValue];

to this:

cell.sliderLevel.value = [d nodeLevel];


[d nodeLevel] returns an integer, a primitive type, not an Objective-C object. Therefore, you cannot call -intValue on it, and that's why you get an EXC_BAD_ACCESS.

Further, the reason your slider is maxed out is because you haven't set its maximum value to 2.0. Because it defaults to 1.0, when you set it to any value 1.0 or higher, it will appear maxed out (in your case, both 1 and 2 appear the same). At some point, you need to call cell.sliderLevel.maximumValue = 2.0; to make the maximum value possible high enough.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号