chr*_*s P 2 cocoa objective-c nsarray nspredicate
我知道你可以做一些像[..."SELF.some_id == [c]%d AND SELF.some_id == [c]%d",id1,id2],但我还需要更多.有没有办法在不构建字符串的情况下执行此操作.
例如...
NSArray *arrayOfWantedWidgetIds = @[1,3,5,6,9,13,14,16];
NSMutableArray *allWidgets = [[WidgetManager sharedWidget] getAllWidgets];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.widget_id ==[c] %d", arrayOfWantedWidgetIds]; //obviously we can't do this, it won't accept an array of IDS for %d
[allWidgets filterArrayUsingPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这样的目标?另一种方式......如果我循环这个,并为arrayOfWantedWidgetIds中的每个值创建单独的谓词,然后将所有单个谓词添加到数组中......这也无济于事,因为filterArrayUsingPredicate只接受NSPredicate.不是他们的数组.
您不能像这样将数组传递给谓词API.但是,您可以传递一组ID,并使用IN运算符,这在您的特定情况下应该足够了:
NSArray *arrayOfWantedWidgetIds = @[@1, @3, @5, @6, @9, @13, @14, @16];
NSMutableArray *allWidgets = [[WidgetManager sharedWidget] getAllWidgets];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.widget_id IN %@", arrayOfWantedWidgetIds];
[allWidgets filterArrayUsingPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)
您也可以使用构造复合谓词NSCompoundPredicate,但在这种情况下这是不必要的.
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |