简体   繁体   English

EXEC_BAD_ACCESS(代码= 1)

[英]EXEC_BAD_ACCESS(Code=1)

Everything was working fine till I added a new NSNUmber variable. 一切正常,直到我添加了一个新的NSNUmber变量。

My .h file looks has this - 我的.h文件看起来具有以下内容-

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

and in my .m file I have - 在我的.m文件中-

@synthesize foodPriceTotal;

In my Initwithnibname method I do this - 在我的Initwithnibname方法中,我这样做-

foodPriceTotal = [[NSNumber alloc] init];

In my cellForRowAtIndexPath function I do this 在我的cellForRowAtIndexPath函数中,我这样做

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 - 上下滚动时出现错误EXEC_BAD_ACCESS(Code = 1)-

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. 正如Till所指出的,您的所有权限定符( assign )不是您想要的。 In 99% with NSNumber you want copy instead. 在使用NSNumber 99%中,您想copy

Then later, use the accessor method: 然后,使用accessor方法:

self.foodPriceTotal = ...

No reason to put that NSNumber *foodPriceTotal declaration into your .h file at all, just delete that line. 完全没有理由将NSNumber *foodPriceTotal声明放入您的.h文件,只需删除该行即可。

Regarding your init method, you might want to rethink about your logic. 关于您的init方法,您可能需要重新考虑您的逻辑。 What is a number without any value here? 什么是没有值的数字? Probably nil is just as good and much clearer. 可能nil一样好,而且更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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