Objective c 执行错误访问(代码=1)
在我添加新的NSNUmber变量之前,一切都很正常 我的.h文件看起来有以下内容-Objective c 执行错误访问(代码=1),objective-c,ios,Objective C,Ios,在我添加新的NSNUmber变量之前,一切都很正常 我的.h文件看起来有以下内容- NSNumber *foodPriceTotal; @property (assign, readwrite) NSNumber *foodPriceTotal; 在我的.m文件中,我有- @synthesize foodPriceTotal; 在我的Initwithnibname方法中,我这样做- foodPriceTotal = [[NSNumber alloc] init]; 在我的cellForRo
NSNumber *foodPriceTotal;
@property (assign, readwrite) NSNumber *foodPriceTotal;
在我的.m文件中,我有-
@synthesize foodPriceTotal;
在我的Initwithnibname方法中,我这样做-
foodPriceTotal = [[NSNumber alloc] init];
在我的cellForRowAtIndexPath函数中,我这样做
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
CGFloat deliveryCharge = [prefs floatForKey:@"deliveryCharge"];
foodPriceTotal = [NSNumber numberWithFloat:([foodPriceTotal floatValue] + deliveryCharge)];
当我上下滚动时,我得到错误EXEC\u BAD\u访问(代码=1)-
错误线是-
foodPriceTotal = [NSNumber numberWithFloat:([foodPriceTotal floatValue] + deliveryCharge)];
有什么想法吗?正如蒂尔所指出的,您的所有权限定符(
assign
)不是您想要的。在99%中,使用NSNumber
您希望改为copy
然后,稍后使用访问器方法:
self.foodPriceTotal = ...
根本没有理由将该声明放入.h文件中,只需删除该行即可
关于
init
方法,您可能需要重新考虑您的逻辑。这里没有任何值的数字是什么?可能nil
同样好而且更清晰。正如蒂尔所指出的,您的所有权限定符(assign
)不是您想要的。在99%中,使用NSNumber
您希望改为copy
然后,稍后使用访问器方法:
self.foodPriceTotal = ...
根本没有理由将该声明放入.h文件中,只需删除该行即可
关于
init
方法,您可能需要重新考虑您的逻辑。这里没有任何值的数字是什么?可能nil
同样好而且更清晰。您可能希望对NSNumber
实例(foodPriceTotal
)使用强属性或弱属性,但不要赋值。谢谢。您对您的评论投了赞成票。您可能希望对您的NSNumber
实例(foodPriceTotal
)使用强属性或弱属性,但不分配。谢谢。谢谢Eiko和Till,谢谢你的评论。我需要阅读更多关于所有权限定符的内容。但是你的建议解决了我的问题。谢谢Eiko和Till。我需要阅读更多关于所有权限定符的内容。但是你的建议解决了我的问题。