2

我在使用谓词过滤数组时遇到问题。我的代码是这个:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSError *error;
NSFileManager *fm = [NSFileManager defaultManager];
NSString *filename=[NSString stringWithFormat:@"%@/%lld-%@.jpg",[paths objectAtIndex:0],[TRIP surrogateKey], ms];

NSArray *dirContents = [fm contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"SELF beginsWith %lld-%@",[TRIP surrogateKey], ms];
NSArray *registros=[dirContents filteredArrayUsingPredicate:fltr];

在调试会话中,我发现我在执行代码的最后一行时遇到问题(NSArray *registros=[dirContents filteredArrayUsingPredicate:fltr]),它给了我以下错误:

-[__NSCFString objCType]: unrecognized selector sent to instance 0x7e67200 2013-02-01 16:37:59.132 GastosApp[4462:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objCType]: unrecognized selector sent to instance 0x7e67200'

在调试部分,我看到内存 0x7e67200 被声明的 ms 变量占用:

NSMutableString *ms = [[NSMutableString new] autorelease];

有没有人看到任何错误?

谢谢并恭祝安康

4

1 回答 1

1

可能是错误的,因为 NSPredicate 会自动引用 %@ 等格式变量。

例如,这个:

[NSPredicate
    predicateWithFormat:@"SELF like[c] %@*%@", prefix, suffix];

将如下:SELF LIKE[c] "prefix" * "suffix"

正确的方法:

[NSPredicate
    predicateWithFormat:@"SELF like[c] %@",
    [[prefix stringByAppendingString:@"*"] stringByAppendingString:suffix]];

这给了我们一个正确的结果:SELF LIKE[c] "prefix*suffix"

于 2013-02-01T16:22:39.710 回答