简体   繁体   中英

Storing NSNumber in NSMutableArray

I have the following declarations in my model.h:

@interface Model: NSObject {
NSMutableArray *myMutableArray;
....
}
@property (nonatomic) double myDouble;

The corresponding @synthesize in model.m:

@synthesize myDouble;

I then have the following setter override:

-(void) setMyDouble: (double) newDouble{
myDouble = newDouble;
[myMutableArray addObject:[NSNumber numberWithDouble:myDouble]];
}

Putting a break point after the array assignment, the debugger shows the following for myMutableArray:

myMutableArray = (_NSArrayM *) 0x631c450 1 objects

0 = (NSCFNumber *) 0x631c6a0

So, my double does not seem to be properly getting into the array. I have subsequent assignments to this array for NSStrings that show up fine in the debugger. The values for both myDouble and newDouble are good (usually just an integer).

I've read several threads on assigning doubles to NSMutableArrays and haven't discovered anything out of the ordinary. Any guidance would be appreciated.

Update

It appears that the code is correct, but I failed to understand that the debugger shows the NSNumber's address rather than its value. Thank you everyone for responding, much appreciated: :)

It seems you are confusing with 0 in 0 = (NSCFNumber *) 0x631c6a0 . That 0 is the index of the NSNumber in the array. If you retrieve the objects from the array and print it in NSLog, it would show you the correct values. Nothing seems to be wrong in your code.

You forget to allocate your mutable array. Debug your app and see if the array is allocated or not.

Edit

Change your function name to something else and see the magic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM