我有以下代码:
- (NSDecimalNumber *)totalBudgetItems {
NSEnumerator *e = [self.defaultBudgetItemsArray objectEnumerator];
id object;
while (object = [e nextObject]) {
NSDecimalNumber *numberToAdd = [[NSDecimalNumber alloc] initWithDecimal:[[object objectForKey:@"actualValue"] decimalValue]];
currentTotal = [currentTotal decimalNumberByAdding:numberToAdd];
[numberToAdd release];
}
return currentTotal;
}
它在我分配numberToAdd的行上崩溃了。在调试器中,我打开“Locals”,然后打开“numberToAdd”。 “currentTotal”为红色,其中包含“无效的CFStringRef”。否则我不确定发生了什么。
在我的.h我有:
NSDecimalNumber *currentTotal;
和
@property (nonatomic, retain) NSDecimalNumber *currentTotal;
控制台还说:
2010-10-06 13:32:07.018 App[9433:307] -[NSCFString objectForKey:]: unrecognized selector sent to instance 0x16c6f0
2010-10-06 13:32:07.030 App[9433:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x16c6f0'
有什么想法吗?
谢谢!
-Max
答案 0 :(得分:0)
问题是您将-decimalValue
发送到返回整数表示的对象。但是,initWithDecimal:
需要一个对象,因此发送消息并崩溃,因为该地址几乎肯定没有对象。
此代码应该有效:
currentTotal = [currentTotal decimalNumberByAdding: [object objectForKey:@"actualValue"]];