ios - 使用 NSPredicate 过滤对象中的数组

标签 ios objective-c nsarray nspredicate

我需要根据另一个数组中对象的属性过滤数组。 假设我有两个类:

商店列表.h

@interface StoreList : NSObject {
  NSMutableArray *storesArray; //Array containing Store objects
}

商店.h

@interface Store : NSObject {
  NSString *name;
}

因此,我有一个 NSArray (storeListArray),其中包含一些 StoreList 对象。然后,我的 Array 是这样的:

storeListArray = [
 StoreList:{
    storesArray: {
                  stores[{
                    store: {
                     name: "Store1"
                    },
                    store: {
                     name: "Store2"
                    }
                  }]
                },
    storesArray: {
                  stores[{
                    store: {
                     name: "Store1"
                    },
                    store: {
                     name: "Store2"
                    }
                  }]
                }
   }
];

那么,我的问题是:如何使用 NSPredicate 通过存储对象的“名称”属性过滤 storeListArray?

我正在尝试做这样的事情,但这不起作用:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY storeList CONTAINS[cd] %@", filterString];
self.filteredStores = [storeListArray filteredArrayUsingPredicate:predicate];
return self.filteredStores;

感谢您的帮助!

最佳答案

试试这个,

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"ANY SELF.storesArray.name == %@", filterString];

NSArray * videoArray = [storeListArray filteredArrayUsingPredicate:predicate];

关于ios - 使用 NSPredicate 过滤对象中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22542970/

相关文章:

android - 如何在 PageView 中设置不同大小的页面?

iphone - MKAnnotation viewForAnnotation 从未被调用

ios - 显示 block 中的下一个 ViewController

objective-c - 从 CGImageRef 获取原始 RGB 缓冲区

ios - 更新 NSarray 内特定键的值

ios - 发送按钮,打开 UIPickerView 到 UIPickerView 的 selectRow 函数

ios - 如何撤消线条描边 ios

ios - 当应用程序在另一个设备上运行时,如何使对象适应另一个大小。 ( swift )

objective-c - 每次需要访问特定索引处的元素时是否可以重新排序数组?

iphone - 将 c-struct 放入 NSArray 的最佳方法是什么?