【问题标题】:Save the result of setPredicate to NSString将 setPredicate 的结果保存到 NSString
【发布时间】:2018-08-16 14:42:06
【问题描述】:

我创建了 NSPredicate 来搜索我的核心数据中的字符串。 所以在这种情况下,有谁知道如何将结果 [fetchRequest setPredicate:predicate]; 保存在 NSString 中。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",
                                  mytext];
[fetchRequest setPredicate:predicate];

【问题讨论】:

  • setPredicate: 不返回任何内容。你能明确你的问题吗?样品?
  • 嗨@Larme,我有结果 [fetchRequest setPredicate:predicate];所以我想将该结果保存在 NSString 中。
  • 您必须解释一下,获取结果如何可以是字符串。获取实体,而不是字符串,也没有获取一个,但可能是 N。
  • 在 fetch 中搜索的结果给出了字符串是否包含的结果。所以如果它包含它会显示在字符串中包含一个的结果。

标签: ios objective-c nsstring nspredicate


【解决方案1】:

@"YOUR_ENTITY_NAME"中写下你的表名

确保您的表包含name

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"YOUR_ENTITY_NAME"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", mytext]];
NSMutableArray *arrResult = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSLog(@"%@", arrResult); //output

【讨论】:

    【解决方案2】:

    我想这会对你有所帮助

    NSArray *fetchedObjects;
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"your entity"  inManagedObjectContext: context];
    [fetch setEntity:entityDescription];
    [fetch setPredicate:[NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", mytext]];
    NSError * error = nil;
    fetchedObjects = [context executeFetchRequest:fetch error:&error];
    
    if([fetchedObjects count] == 1)
    NSLog(@"%@",[fetchedObjects objectAtIndex:0]);
    

    【讨论】:

    • HI @lyankar,不幸的是它崩溃了,并且消息:我未实现谓词的 SQL 生成
    • 这里究竟是什么获取请求 [fetchRequest setPredicate:predicate];
    • 感谢@lyankar 和 Nirav Kotecha。
    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 2017-05-07
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多