ios - 从对象中检索值

标签 ios objective-c

我有以下对象,其中包含 NSDictionary 数组。我想知道如何从该对象中获取 Quantity 的总和。

enter image description here

最佳答案

在 Objective-C 中,有一种使用KVC 集合运算符 的非常方便的方法:

NSNumber *sum = [sharedData.orderItems valueForKeyPath:@"@sum.Quantity.integerValue"];
NSInteger integerSum = sum.integerValue;

其他简单的运算符是:

  • @count - 对象数
  • @avg - 平均值
  • @max - 最大值
  • @min - 最小值

关于ios - 从对象中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44476804/

相关文章:

ios - 当应用程序被用户强制退出时处理推送通知 iOS(替代方案?)

android - 从 PhoneGap/Cordova 应用程序切换到手机的 native 联系人(查看)应用程序

ios - Storyboard中动态大小的 UITextView

iphone - 使用 NSCalendar——获取下个月的第一个星期二

objective-c - objectValueForTableColumn : get called? 何时出现

ios - 通过我的应用程序打开 Instagram 应用程序

ios - 在长时间运行的进程后台期间防止 EXC_BAD_ACCESS

ios - Bundle.preferredLocalizations 困惑

iphone - NSFetchedResultsController : Fill TableView with fetched entity and its related entity

ios - 如何将用户标识保存在 nsuserdefaults 中?