简体   繁体   中英

EXEC_BAD_ACCESS(Code=1)

Everything was working fine till I added a new NSNUmber variable.

My .h file looks has this -

NSNumber *foodPriceTotal;
@property (assign, readwrite) NSNumber *foodPriceTotal;

and in my .m file I have -

@synthesize foodPriceTotal;

In my Initwithnibname method I do this -

foodPriceTotal = [[NSNumber alloc] init];

In my cellForRowAtIndexPath function I do this

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
CGFloat deliveryCharge = [prefs floatForKey:@"deliveryCharge"];
foodPriceTotal = [NSNumber numberWithFloat:([foodPriceTotal floatValue] + deliveryCharge)];

I get error EXEC_BAD_ACCESS(Code=1) when I scroll up and down -

The erring line is -

foodPriceTotal = [NSNumber numberWithFloat:([foodPriceTotal floatValue] + deliveryCharge)];

Any idea please?

As Till pointed out, your ownership qualifier ( assign ) is not what you want here. In 99% with NSNumber you want copy instead.

Then later, use the accessor method:

self.foodPriceTotal = ...

No reason to put that NSNumber *foodPriceTotal declaration into your .h file at all, just delete that line.

Regarding your init method, you might want to rethink about your logic. What is a number without any value here? Probably nil is just as good and much clearer.

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