【问题标题】:Apply filteredArrayUsingPredicate on specific column of a multidimensional NSMutableArray在多维 NSMutableArray 的特定列上应用 filteredArrayUsingPredicate
【发布时间】:2011-12-05 17:33:15
【问题描述】:

我有一个多维可变数组,其中包含每个维度中的各种对象。

位置 1 = foodID int,位置 2 = itemName NSString

即。

   Row 1     [1, "Jif"];    
   Row 2     [2, "Skippy"];
   Row 3     [3, "Peter Pan"];

当我尝试使用下面的以下代码时,它会引发异常,因为它会尝试将位置 1 评估为字符串

//---get all Peanut Butter Types beginning with the letter---
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];    
NSArray *beginWith = [food filteredArrayUsingPredicate:predicate];

例外

'NSInvalidArgumentException', reason: 'Can't do a substring operation with something that isn't a string'

谁能帮助我如何使用谓词过滤多维数组的特定列?

NSDictionary 有很多这样的例子,它可以应用于键,但我似乎无法将它应用于数组中的特定位置。

【问题讨论】:

    标签: objective-c xcode nsmutablearray nspredicate


    【解决方案1】:

    您正在尝试使用谓词查找或过滤数组,其中您的主数据源(“食物”)数组具有数组类型的对象。并且在您的谓词公式中试图查找字符串对象,但在“食物”数组中,每个对象都有数组,因此它崩溃了并且无法过滤数组。希望您能从这个 cmets 中理解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 2020-01-08
      • 2014-08-13
      相关资源
      最近更新 更多