我正在尝试替换数组中的文本,由于某种原因,尽管 Xcode 没有发出行警报,但代码无法正常工作,这会导致其崩溃。
即“名字,2013”
剥离“,2013”
结果
“姓名”
我做错了什么,我一生都无法解决这个问题,并且迫切需要帮助?
有问题的具体行是“case ITSectionTypeAuthor:”
- (void)setUpDataByType:(ITSectionType) type andFilter:(NSString *)filter {
self.type = type;
self.filter = (filter)? filter : @"";
[self.sections removeAllObjects];
NSPredicate *predicate = nil;
NSString *descriptorKey = @"genus";
NSArray *rowData = [[ITData sharedObject] animals]; //coming form singleton
//generate predicate
switch (self.type) {
case ITSectionTypeAuthor:
predicate = [NSPredicate predicateWithFormat:@"describer componentsSeparatedByString:@", "] objectAtIndex:0] LIKE %@", filter];
break;
default:
predicate = nil;
break;
}
最佳答案
我认为问题出在这一行:
predicate = [NSPredicate predicateWithFormat:@"describer componentsSeparatedByString:@", "] objectAtIndex:0] LIKE %@", filter];
您正在创建一个 Objective-C 字符串:
@"describer componentsSeparatedByString:@"
然后是 C 字符串:
"] objectAtIndex:0] LIKE %@"
这不对吧?我想你想要:
predicate = [NSPredicate predicateWithFormat:@"describer LIKE %@", [[filter componentsSeparatedByString:@", "] objectAtIndex:0]];
关于ios - 替换 NSPredicate 中字符串中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16970452/