我正在尝试将 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 中,以便对其进行排序。
更新:这就是我调用 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);
但在最后一行代码中,我有奇怪的输出。