NSMutableString的内存问题

时间:2013-02-01 15:58:20

标签: ios objective-c memory nspredicate

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

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];

有没有人看到任何错误?

谢谢和问候

1 个答案:

答案 0 :(得分: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"