我在 NSMutableArray 上使用 NSPredicate,如下所示,从 XML 表中填充
//Get XML Data
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"breosla_db" ofType:@"xml"];
NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLFile:filePath];
//Place in Mutable Array
NSMutableArray *data = [xmlDoc valueForKeyPath:@"row"];
NSLog(@"Data %@", data[0]);
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"ID contains[c] '2'"];
NSArray *filtered = [data filteredArrayUsingPredicate:bPredicate];
日志中的数据如下所示:
Data {
ID = (
2
);
address1 = (
"123 Fake Street"
);
address2 = (
"A Sleepy Town"
);
address3 = (
);
county = (
);
licenseNo = (
12345
);
licenseType = (
"Trader"
);
licenseeName = (
"Mr. Trader"
);
nominessName = (
);
tradingName = (
"Trading Shop"
);
当前谓词:
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"ID contains[c] '2'"];
只返回 ID == 2 的数据,不返回 12、22、42 等。
其次,我无法通过例如所有包含“e”的名称:
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"tradingName contains[c] 'e'"];
'data' NSMutableArray,在每个位置,包含类似于日志中显示的数据。
最佳答案
看起来您的数据包含一个字典数组,每个字典键的值也是(单个元素)数组。您可以看到这一点,因为您的日志显示
ID = (
2
);
etc
( ) 表示数组。因此,要使谓词起作用,您需要使用“ANY”,如下所示:
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"ANY ID contains[c] '2'"];
或
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"ANY tradingName contains[c] 'e'"];
但是您可能会更好地重构数据,使值不嵌入数组中。
关于ios - NSPredicate - 包含未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27218878/