我有一个NSMutableDicitonary
,我将该字典中的对象保存到NSMutableArray
。
我的问题是,当我尝试将NSMutableArray
崩溃的总和加起来
self.viewOrderSummaryDictionary = [[NSMutableDictionary alloc] init];
[self.viewOrderSummaryDictionary setObject: [self.viewSelectedItemsArray valueForKey:@"MenuItemName"] forKey: @"description"];
[self.viewOrderSummaryDictionary setObject: [self.viewSelectedItemsArray valueForKey:@"MenuItemImage"] forKey: @"imageUrlString"];
[self.viewOrderSummaryDictionary setObject: [self.viewSelectedItemsArray valueForKey:@"MenuItemPrice"] forKey: @"price"];
[self.viewOrderSummaryDictionary setObject: [self.viewSelectedItemsArray valueForKey:@"MenuItemCurrency"] forKey: @"currency"];
[self.viewOrderSummaryDictionary setObject: [self.viewSelectedItemsArray valueForKey:@"MenuItemId"] forKey: @"id"];
[self.viewOrderSummaryDictionary setObject: [self.viewSelectedItemsArray valueForKey:@"Quantity"] forKey: @"quantity"];
当我填充我的UITableview时,我得到浮动值崩溃
NSDictionary* currentItem = [self.viewOrderSummaryArray objectAtIndex: indexPath.row];
cell.productNameLabel.text = [currentItem objectForKey: @"description"];
cell.productPriceLabel.text = [NSString stringWithFormat: @"%@%@ x %@",[currentItem objectForKey: @"currency"],[currentItem objectForKey: @"price"], [currentItem objectForKey: @"quantity"]]; // TODO: Must implement this later
[cell.productImageView sd_setImageWithURL:[currentItem objectForKey: @"imageUrlString"] placeholderImage:[UIImage imageNamed:@"appPlaceholderImage"]];
CALayer * l = [cell.productImageView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
float sumq = 0;
NSArray *array = [self.viewOrderSummaryArray valueForKey:@"price"];
for (NSNumber *num in array)
{
sumq += [num floatValue];
}
float currentPrice = [[currentItem objectForKey: @"price"] floatValue];
float quantity = [[currentItem objectForKey: @"quantity"] intValue];
sumPrice += (currentPrice * quantity);
这是我的NSMutableArray的样子:
(
{
currency = (
R,
R
);
description = (
MenuItem,
MenuItem
);
id = (
3,
2
);
imageUrlString = (
"http://www.ynet.co.il/PicServer2/24012010/2400206/1_wa.jpg",
"http://photos-c.ak.fbcdn.net/hphotos-ak-prn2/t1.0-0/c1.0.959.639/s720x720/1509226_791526100861957_869094305_n.jpg"
);
price = (
"59.95",
"59.95"
);
quantity = (
1,
1
);
}
以下是崩溃日志:
2014-10-07 07:28:37.231 AppNinja[12824:1500013] -[__NSArrayI floatValue]: unrecognized selector sent to instance 0x7f98b0da73e0
2014-10-07 07:28:37.233 AppNinja[12824:1500013] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI floatValue]: unrecognized selector sent to instance 0x7f98b0da73e0'
*** First throw call stack:
(
0 CoreFoundation 0x00000001048a03f5 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000104455bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001048a750d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001047ff7fc ___forwarding___ + 988
4 CoreFoundation 0x00000001047ff398 _CF_forwarding_prep_0 + 120
5 DeliNinja 0x0000000101921580 -[ViewOrderSummaryViewController tableView:cellForRowAtIndexPath:] + 2000
6 UIKit 0x0000000102e09cd3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
7 UIKit 0x0000000102de97f1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
8 UIKit 0x0000000102dff65c -[UITableView layoutSubviews] + 213
9 UIKit 0x0000000102d8c199 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
10 QuartzCore 0x0000000102a5ff98 -[CALayer layoutSublayers] + 150
11 QuartzCore 0x0000000102a54bbe _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
12 QuartzCore 0x0000000102a54a2e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
13 QuartzCore 0x00000001029c2ade _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
14 QuartzCore 0x00000001029c3bea _ZN2CA11Transaction6commitEv + 390
15 QuartzCore 0x00000001029c4255 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
16 CoreFoundation 0x00000001047d5347 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
17 CoreFoundation 0x00000001047d52a0 __CFRunLoopDoObservers + 368
18 CoreFoundation 0x00000001047cb0d3 __CFRunLoopRun + 1123
19 CoreFoundation 0x00000001047caa06 CFRunLoopRunSpecific + 470
20 GraphicsServices 0x00000001059a99f0 GSEventRunModal + 161
21 UIKit 0x0000000102d13550 UIApplicationMain + 1282
22 DeliNinja 0x00000001019b4a73 main + 115
23 libdyld.dylib 0x00000001065f2145 start + 1
)
答案 0 :(得分:1)
您在floatValue
而非NSArray
对象上呼叫NSNumber
。
验证:
for (NSNumber *num in array)
{
NSLog(@"num is class %@", NSStringFromClass([num class]));
sumq += [num floatValue];
}
答案 1 :(得分:0)
我不是百分百肯定,但我认为你想要的是:
for (NSDictionary *dict in self.viewOrderSummaryArray)
{
NSNumber* n = [val valueForKey:@"price"];
sumq += [num floatValue];
}