如何从核心数据实体中读取NSArray

时间:2013-11-07 10:36:42

标签: ios core-data nsmutablearray nsarray nssortdescriptor

我正在尝试将一个coredata实体读入一个NSMutableArray但是我一直在收回奇怪的数据。例如,当我找回值的NSDictionary时,它看起来像这样

data: {
    companyName = "WPremium";
    desc = "Test";
    guid = "Otq12342";
    install =     (
        "0x1e59e910 <x-coredata://BAF7B1AD-F357-455A-B9D7-1288F7D1F652/Install/p851>",
        "0x1e59e8e0 <x-coredata://BAF7B1AD-F357-455A-B9D7-1288F7D1F652/Install/p848>",
        "0x1e59e830 <x-coredata://BAF7B1AD-F357-455A-B9D7-1288F7D1F652/Install/p837>",
        "0x1e59e930 <x-coredata://BAF7B1AD-F357-455A-B9D7-1288F7D1F652/Install/p853>",
        "0x1e59e850 <x-coredata://BAF7B1AD-F357-455A-B9D7-1288F7D1F652/Install/p839>",
        "0x1e59e890 <x-coredata://BAF7B1AD-F357-455A-B9D7-1288F7D1F652/Install/p843>",
        "0x1e59e8b0 <x-coredata://BAF7B1AD-F357-455A-B9D7-1288F7D1F652/Install/p845>",
        "0x1e59e7c0 <x-coredata://BAF7B1AD-F357-455A-B9D7-1288F7D1F652/Install/p830>",
        "0x1e5957e0 <x-coredata://BAF7B1AD-F357-455A-B9D7-1288F7D1F652/Install/p829>",
        "0x1e59e810 <x-coredata://BAF7B1AD-F357-455A-B9D7-1288F7D1F652/Install/p835>",
        "(...and 16 more...)"
    );

我想知道如何将coredata字典的安装对象放入NSArray中,以便对其进行排序。任何帮助都会受到赞赏。

UPDate这就是我调用我的coredata对象的方式。

- (NSMutableArray *)readSelectedInstall:(NSString *)projIDString {
    NSManagedObjectContext *context = [self managedObjectContext];


    if (context == nil) {
        NSLog(@"Nil");
    }
    else {


        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"InstallProject" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"projID==%@",projIDString];
        [fetchRequest setPredicate:predicate];

        NSError *error;

        NSMutableArray *installProjectDictionaryArray = [[NSMutableArray alloc] init];


        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
        for (InstallProject *installProj in fetchedObjects) {

            NSMutableDictionary *tempInstallProjectDictionaryArray = [[ NSMutableDictionary alloc] init];

            [tempInstallProjectDictionaryArray setObject:installProj.companyName forKey:@"CompanyName"];
            [tempInstallProjectDictionaryArray setObject:installProj.projNo forKey:@"ProjNo"];
            [tempInstallProjectDictionaryArray setObject:installProj.desc forKey:@"Desc"];
            [tempInstallProjectDictionaryArray setObject:installProj.guid forKey:@"GUID"];
            [tempInstallProjectDictionaryArray setObject:installProj.projID forKey:@"ProjID"];
            [tempInstallProjectDictionaryArray setObject:installProj.install forKey:@"install"];

            [installProjectDictionaryArray addObject:tempInstallProjectDictionaryArray];
        }

        return installProjectDictionaryArray;
    }
    return nil;
}

然后返回NSMutableArray我这样做

NSMutableArray *getarray = [self readSelectedInstall:projIDString];

    NSMutableDictionary *installsDictionary = [getarray objectAtIndex:0];

    NSMutableArray *tempInstalls = [installsDictionary objectForKey:@"install"];
    NSLog(@"%@", tempInstalls);

    NSArray *tempSortedItemsArray = [tempInstalls sortedArrayUsingDescriptors:
                                     @[[NSSortDescriptor
                                        sortDescriptorWithKey:@"dp" ascending:YES],
                                     [NSSortDescriptor
                                      sortDescriptorWithKey:@"dc

" ascending:YES]]];

    tempInstalls = [tempSortedItemsArray mutableCopy];
    tempSortedItemsArray = nil;




    NSLog(@"%@", tempInstalls);

但是在最后一行代码中,我有一个奇怪的输出..

1 个答案:

答案 0 :(得分:1)

您需要做的就是从Core Data中获取核心数据NSManagedObject,安装将作为NSSet返回,看起来它是NSManagedObjects的NSSet。

获得对象后,您可以访问“install”属性并按需要排序。

// Get the Core Data Object (Uses Magical Record)
MyObject *object = [MyObject MR_findFirstWithPredicate:[NSPredicate objectPredicate:self.users_id]];
// Sort the object set into an array
NSArray *installs = [object.install sortedArrayUsingDescriptors:@[ [NSSortDescriptor sortDescriptorWithKey:k_timestamp ascending:YES] ]];