我当前的项目遇到了障碍。我基本上有一个非常类似于 Core Data Recipe 应用程序的应用程序......
这是我的 .xcdatamodel 中的基本结构
实体:餐厅
字符串:名称
Category:类别
实体:类别
字符串:名称
Restaurant:餐厅
“外卖”和“家庭用餐”是 10 种可能的餐厅类别中的 2 种。
我该怎么做呢..我查看了 sqllite 数据库,我有我的 ZRestaurant 和 ZCategory 表 + 它们两个的连接表...我都有数据...
如何使用不同的值设置我的餐厅类别?然后我该如何取回它们?
谢谢大家!
库尔特
好的,经过过去两天的研究,我终于想出了我的解决方案,它实际上是亚历克斯和威尔斯建议的混合......谢谢你们俩!
这是我所拥有的...
NSManagedObjectContext *context = [restaurant managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:context]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *possibleCategories = [context executeFetchRequest:fetchRequest error:&error];
categoryArray = [[NSMutableArray alloc] initWithArray:possibleCategories];
currentCategories = [restaurant valueForKeyPath:@"categories"];
[restaurant addCategoriesObject:(Category *)[possibleCategories objectAtIndex:15 ]];
[currentCategories addObject:(Category*)[categoryArray objectAtIndex:15]];
然后我像这样保存
- (void)save{
NSLog(@"EditCatagoriesTableViewController - save");
NSSet* myCategorySet = [[NSSet alloc] initWithSet:currentCategories];
NSError *error = nil;
[restaurant addCategories:myCategorySet];
error = nil;
if (![restaurant.managedObjectContext save:&error]) {
// Handle error
NSLog(@"restaurant - Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
就这样!
非常感谢两位的帮助!!!
-Kurt
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)