0

我有一個NSMutableDicitonary,其中我將該對象從該字典保存到NSMutableArray嘗試使用浮點值時應用程序崩潰IOS

我的問題是,當我嘗試添加了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 
    ); 
} 

這裏是崩潰lo G:

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

確保您添加對象的值(的NSString或NSNumber的舉例) 。 setObject:需要id值 – 2014-10-06 14:14:04

+0

崩潰日誌中的錯誤消息和相關信息在哪裏? – rmaddy 2014-10-06 14:16:03

+0

爲什麼你使用floatValue而不是doubleValue?只要你的數字變大一點,你就會得到顧客抱怨的四捨五入錯誤。 – gnasher729 2014-10-06 16:30:55

回答

1

要調用上NSArray不是NSNumber對象floatValue

驗證:

for (NSNumber *num in array) 
{ 
    NSLog(@"num is class %@", NSStringFromClass([num class])); 
    sumq += [num floatValue]; 
} 
0

我不是100%肯定,但我想你想要的是:

for (NSDictionary *dict in self.viewOrderSummaryArray) 
{ 
    NSNumber* n = [val valueForKey:@"price"]; 
    sumq += [num floatValue]; 
} 
相關問題