iphone - 如何从核心数据中获取唯一字段值(song_type)

标签 iphone ios core-data ios4

我想知道如何从核心数据中获取唯一字段值(song_type)。

我有下面的代码

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Songs" inManagedObjectContext:myManagedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
    [fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:@"song_type"]]; 
    [fetchRequest setEntity:entity];

    NSError *aError;

    NSMutableArray *fetchedResultsArray = [[myManagedObjectContext executeFetchRequest:fetchRequest error:&aError]mutableCopy];

    if(! fetchedResultsArray ){
        NSLog(@"EEEERRROR");
    }
    else {
        NSLog(@"fetched %d", [fetchedResultsArray count]);
    }

    [fetchedResultsArray release];
    [fetchRequest release];

我收到以下错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid keypath song_type passed to setPropertiesToFetch:'

最佳答案

设置以下 2 个属性,您就完成了。

fetchRequest.returnsDistinctResults = YES;
fetchRequest.resultType = NSDictionaryResultType;

希望这对您有所帮助。

编辑

NSFetchRequest *fetchRequest= [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Songs" inManagedObjectContext:myManagedObjectContext];

//Take properties dictionary
NSDictionary *entityProperties = [entity propertiesByName];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:@"song_type"]]];
NSArray * result = [myManagedObjectContext executeFetchRequest:fetchRequest error:nil];

关于iphone - 如何从核心数据中获取唯一字段值(song_type),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306950/

相关文章:

iphone - 检查邮件设置是否通过代码在 IOS 设备上完成

ios - 更新 Realm 上的对象

objective-c - 如何在 iOS 应用程序中使用自定义字体?

ios - 如何管理具有 6 个实体和关系的大型 ManagedObjectModel?核心数据

iphone - 核心数据、UITableView 和 UISegmentedControl

javascript - iPhone 中表格单元格的触摸事件

iPhone代码-CGPoint分配问题

javascript - iPhone safari 底部栏

iphone - 使用 AVAudioPlayer 快进歌曲

ios - 休息套件 : addSQLitePersistentStoreAtPath produce extra record