【发布时间】:2014-04-27 04:46:40
【问题描述】:
我的数据模型如下: 类别
—>名字
—>items(“汽车”类型的项目数组)
我正在尝试使用部分作为名称来获取,并且行应该是项目(这是一个 NSArray)
这是我的代码,我希望将行作为 Car 返回,但它仍然返回“Category”。那么,什么是 NSFetchRequest?
NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Categories"];
NSSortDescriptor *sortDesc=[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDesc]];
fetchController=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:delegate.managedContext sectionNameKeyPath:@"name" cacheName:nil];
NSError *fetchError;
[fetchController performFetch:&fetchError];
我已附上模型的图片。
取车代码如下。
AppDelegate *del=(AppDelegate*)[UIApplication sharedApplication].delegate;
NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Car"];
NSSortDescriptor *sortDesc=[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDesc]];
fetchController=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:del.managedContext sectionNameKeyPath:@"Categories.name" cacheName:nil];
NSError *fetchError;
[fetchController performFetch:&fetchError];
代码在这里上传https://www.dropbox.com/s/jjk0nve3pmytui2/TestAppData%202.zip
注意:- 我正在考虑显示空白部分,因为我可以查看具有“添加”按钮的标题。所以,通过这个用户可以很容易地知道类别的数量。
为了实现上面“注意”中提到的事情,我尝试了以下方法。我创建了一个CarViewController2。它创建了相同数量的类别,但行数取决于categoriesRelationship(这是一个NSOrderedSet)。可以通过取消注释 ViewController.m 的 (IBAction) addCategory:(id) sender 中的一些代码来测试这一点。 (取消评论一次,之后再评论)代码已经更新,可以下载了。
如果需要,请更正代码。谢谢。
【问题讨论】:
标签: ios core-data nsfetchedresultscontroller nsfetchrequest