开发者

Storing NSNumber in NSMutableArray

开发者 https://www.devze.com 2023-03-16 05:28 出处:网络
I have the following declarations in my model.h: @interface Model: NSObject { NSMutableArray *myMutableArray;

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:my开发者_运维百科Double]];
}

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.

0

精彩评论

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