ios - 替换 NSPredicate 中字符串中的内容

标签 ios objective-c

我正在尝试替换数组中的文本,由于某种原因,尽管 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/

相关文章:

iphone - EXC_BAD_ACCESS 与 Objective-C 属性

objective-c - 我的循环有什么问题 - 循环浏览 View

ios - 如何在 iOS 模拟器中禁用系统位置警报

ios - 如何在快速打印 NSDictionary 时打印中文而不是 unicode

ios - willMoveToParentViewController 和 didMoveToParentViewController

ios - 确认密码的正则表达式

ios - 如何从 OAuth2 请求响应中获取数据

ios - 新电脑上的 Xcode "valid signing identity not found"

ios - 使用 Objective-C 计算年龄

ios - 在iOS和OSX之间共享CGColor