objective c - Problems with the loop of the array -
i new in ios. making app in getting data parse back-end working fine. application order , delivery of food. have cart add items. if product in cart added again, should increase quantity , not create 1 in basket. tried implement aid of loop not working should. product added instead of increasing quantity. hope assistance.
+ (void)additem:(pfobject *)item { cart *cart = [cart sharedinstance]; (nsuinteger = 0; < [cart.items count]; i++) { if ([item valueforkey:@"objectid"] == [cart.items[i] valueforkey:@"objectid"]) { nsdecimalnumber *sumqtynew = [nsdecimalnumber decimalnumberwithmantissa:1 exponent:0 isnegative:no]; nsdecimalnumber *sumqty = [nsdecimalnumber decimalnumberwithmantissa:1 exponent:0 isnegative:no]; sumqtynew = [item valueforkey:@"qty"]; sumqty = [cart.items[i] valueforkey:@"qty"]; sumqty = [sumqty decimalnumberbyadding:sumqtynew]; [[cart.items[i] valueforkey:@"objectid"] setobject:sumqty forkey:@"qty"]; } else { [cart.items addobject:item]; } } nsdecimalnumber *plus = [[nsdecimalnumber alloc]initwithstring:[item objectforkey:@"price"]]; cart.totalprice = [cart.totalprice decimalnumberbyadding:plus];
}
if had use cart.items
nsmutalbearray
, here answer
+ (void)additem:(pfobject *)item { cart *cart = [cart sharedinstance]; nsmutablearray * cartitems = cart.items nsstring *objectid = [item valueforkey:@"objectid"]; nspredicate *predicate = [nspredicate predicatewithformat:@"objectid = %@", objectid]; pfobject *existingitem = [[cartitems filteredarrayusingpredicate:predicate] firstobject]; if (existingitem == nil){ [cartitems addobject:item]; } else { int sumqty = [[existingitem valueforkey:@"qty"] intvalue] + [[item valueforkey:@"qty"] intvalue]; [existingitem setobject:[nsnumber numberwithint:sumqty] forkey:@"qty"]; } }
Comments
Post a Comment