【问题标题】:Fetching data from entity using nspredicate使用 nspredicate 从实体中获取数据
【发布时间】:2015-08-18 14:10:54
【问题描述】:

我有一个名为“Songs”的核心数据实体,其中包含不同歌曲详细信息的详细信息。该实体的属性之一是“语言”。我想用西班牙语获取所有歌曲。但是如果西班牙语歌曲的数量为零,那么它应该获取所有默认语言的歌曲,那就是英语。如果我知道默认语言和所需语言,这是否可以通过单个 NSPredicate 实现。

【问题讨论】:

    标签: ios xcode core-data nspredicate


    【解决方案1】:

    你可以这样做。

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Songs" inManagedObjectContext:managedObjectContext];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
    
        //here strLanguage is string with the name of selected language
        NSPredicate * predicate  = [NSPredicate predicateWithFormat:@"Language == %@",strLanguage]; 
        [request setPredicate:predicate];
        [request setEntity:entity];
    
        NSMutableArray* mutableFetchCategory = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    
    if ([mutableFetchCategory count] > 0) 
        {
    
            for (NSManagedObject *info in mutableFetchCategory)
            {
                 NSLog(@"%@",info);
                 //Extract data from  each "info" object and store it in an array and display it in tableview
            }
    }
    
    else
    
    {
        // Repeat the same code [or u can use functions for reusability] with language "English".
    }
    

    你也可以参考这个link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-25
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      相关资源
      最近更新 更多