如何将获取的对象保存到核心数据

时间:2012-10-22 21:00:25

标签: objective-c ios core-data

我非常擅长编程,特别是客观的c核心数据。我正在尝试从电子表格中读取数据并将其显示在tableview中。我创建的用于保存对象的数组是空的。我不确定如何将数据保存到阵列。另外,我收到一条警告,表示存储的值永远不会被读取。我非常感谢你对此提供任何帮助 - 我的智慧结束了。

这是我的代码:

DataStorage *parentTab;
parentTab = (DataStorage *)self.parentViewController;
_context = [parentTab context];

[self populateCollegeObjects];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *dataEntity = [NSEntityDescription entityForName:@"CollegeList"     inManagedObjectContext:_context];
[fetchRequest setEntity:dataEntity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"collegeName" ascending:YES];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors ];
[sortDescriptors release];
[sortDescriptor release];

NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:nil];
[fetchRequest release];

NSError *error;
[controller performFetch:&error];

for (int counter = 0; counter< [sortDescriptors count]; counter ++)
{
   if ([_collegeObjects count] == 0)
    {
        _collegeObjects = [[NSArray alloc] init];

        CollegeList *addCollege;

        //addCollege = [CollegeList initWithName:<#(NSString *)#> andURL:<#(NSString *)#> andDetail:<#(NSString *)#> forContext:<#(NSManagedObjectContext *)#>_context];
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定你的问题到底是什么。数组是否填充了核心数据中的实体?

也许你可以试试看这是否有帮助:

[fetchRequest setEntity:dataEntity];
NSArray *fetchedObjects = [_context executeFetchRequest: fetchRequest error: nil];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"collegeName" ascending:YES];

NSArray *sortDescriptors = @[sortDescriptor];
NSArray *sortedFetchedObjects = [fetchedObjects sortedArrayUsingDescriptors:sortDescriptors];

sortedFetchedObjects应该包含您要查找的已排序对象的数组。