OStack程序员社区-中国程序员成长平台

标题: ios - 如何从 coredata 实体中读取 NSArray [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:09
标题: ios - 如何从 coredata 实体中读取 NSArray

我正在尝试将 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 字典的 install 对象放入一个 NSArray 中,以便对其进行排序。

更新:这就是我调用 coredata 对象的方式。

- (NSMutableArray *)readSelectedInstallNSString *)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"rojNo"];
            [tempInstallProjectDictionaryArray setObject:installProj.desc forKey"Desc"];
            [tempInstallProjectDictionaryArray setObject:installProj.guid forKey"GUID"];
            [tempInstallProjectDictionaryArray setObject:installProj.projID forKey"rojID"];
            [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);

但在最后一行代码中我有奇怪的输出。



Best Answer-推荐答案


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

获得对象后,您可以访问“安装”属性并根据需要进行排序。

// 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] ]];

关于ios - 如何从 coredata 实体中读取 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19833681/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4