【问题标题】:Is there a function similar to the sql "NOT IN" in Core Data?Core Data中是否有类似sql“NOT IN”的功能?
【发布时间】:2014-03-22 05:36:01
【问题描述】:

SQL 命令

从 GAMELOCALTEAM 不在“TBC”顺序中的游戏中选择 GAMELOCALTEAM 由 GAMELOCALTEAM 提供

我想在objective c 中编写sql 命令。下面是我的文字代码

NSManagedObjectContext * context = ((AFLAppDelegate *)[[UIApplication sharedApplication] delegate] ).managedObjectContext;
NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@“game”];
NSEntityDescription *entity = [NSEntityDescription entityForName:@“game” inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT(gamelocalteam IN 'tbc')"];
[fetch setPredicate:predicate];
NSSortDescriptor *sortDescreptor =[[NSSortDescriptor alloc]initWithKey:@"gamelocalteam" ascending:YES];
[fetch setSortDescriptors:[NSArray arrayWithObject:sortDescreptor]];
NSAttributeDescription* attributeDescription = [entity.attributesByName objectForKey:@"gamelocalteam"];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:attributeDescription, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:attributeDescription]];
[fetch setResultType:NSDictionaryResultType];
NSError *error = nil;
_team = [context executeFetchRequest:fetch error:&error];

如果此通话应用程序崩溃,谓词格式为@"NOT(gamelocalteam IN 'tbc')

NOT IN可以吗?

【问题讨论】:

标签: ios objective-c core-data nspredicate nsfetchrequest


【解决方案1】:

试试看

@"NOT gamelocalteam contains[cd] 'tbc'"

下面的代码

NSManagedObjectContext * context = ((AFLAppDelegate *)[[UIApplication sharedApplication] delegate] ).managedObjectContext;
NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@“game”];
NSEntityDescription *entity = [NSEntityDescription entityForName:@“game” inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:>@"NOT gamelocalteam contains[cd] 'tbc'"];
[fetch setPredicate:predicate];
NSSortDescriptor *sortDescreptor =[[NSSortDescriptor alloc]initWithKey:@"gamelocalteam" ascending:YES];
[fetch setSortDescriptors:[NSArray arrayWithObject:sortDescreptor]];
NSAttributeDescription* attributeDescription = [entity.attributesByName objectForKey:@"gamelocalteam"];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:attributeDescription, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:attributeDescription]];
[fetch setResultType:NSDictionaryResultType];
NSError *error = nil;
_team = [context executeFetchRequest:fetch error:&error];

【讨论】:

    【解决方案2】:

    您的谓词代码如下所示,

    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS[c] %@)",@"someKey", [NSString stringWithFormat:@"Some String"]];
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 2017-08-10
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2012-08-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多