使用Objective-C中的NSPredicate过滤自定义数据类的数组

时间:2016-04-25 05:23:53

标签: ios objective-c nsarray uisearchbar nspredicate

我有一个名为DataClass的自定义数据类,这里有详细信息。

@interface DataClass : NSObject

@property (assign,nonatomic)NSInteger invoiceID;
@property (assign,nonatomic)NSInteger anInvoiceID;
@property (strong,nonatomic)NSString *customerName;
@property (strong,nonatomic)NSString *departmentName;
@property (strong,nonatomic)NSString *typeNumberOfInvoice;
@property (strong,nonatomic)NSString *invoiceDate;
@property (strong,nonatomic)NSString *stockNumber;
@property (strong,nonatomic)NSString *vehicleDetail;
@property (strong,nonatomic)NSString *rONumber;
@property (assign,nonatomic)NSInteger amount;
@property (strong,nonatomic)NSString *color;
@property (strong,nonatomic)NSString *year;
@property (strong,nonatomic)NSString *location;
@property (strong,nonatomic)NSString *carVinNumber;
@property (strong,nonatomic)NSString *dealership;
@property (strong,nonatomic)NSString *technician;
@property (strong,nonatomic)NSData *imageData;
@property (strong, nonatomic)NSMutableArray *arrayOfLocations;

在我的ViewController课程中,我有一个文本字段,我根据文本输入输入并显示。我有一个NSMutableArray名为arrayOfData和filteredDataArray。 arrayOfData包含上述DataClass的对象,但是当我尝试使用此代码执行此操作时,我收到错误

  

'ALL或ANY运算符的左侧必须是NSArray或NSSet。'

     NSPredicate *aToZPredicate =
    [NSPredicate predicateWithFormat:@"ANY invoiceID CONTAINS[cd] %@", searchText];
    filteredDataArray = [arrayOfData filteredArrayUsingPredicate:aToZPredicate];
    [_tableView reloadData];

2 个答案:

答案 0 :(得分:1)

试试这个

NSPredicate *aToZPredicate =
[NSPredicate predicateWithFormat:@"CAST(SELF.invoiceID, 'NSString') contains[cd] %@", searchText];
filteredDataArray = [arrayOfData filteredArrayUsingPredicate:aToZPredicate];
[_tableView reloadData];

答案 1 :(得分:1)

如果您要与Integer进行比较,请不要使用contains。与等号比较

NSPredicate *aToZPredicate =
        [NSPredicate predicateWithFormat:@"invoiceID == %d", [searchText intValue]];
        filteredDataArray = [arrayOfData filteredArrayUsingPredicate:aToZPredicate];
        [_tableView reloadData];