【发布时间】:2014-06-15 10:17:44
【问题描述】:
您好,我在我的测试项目中使用核心数据。 我能够将 json 中的内容添加到核心数据实体“书”。但是当我尝试检索数据时,我只得到最后一个“数据”(总共 37 个数据)? 我创建了一个 mutablearray 'directors' 来插入每个检索到的值,但这也不起作用。
请检查我的代码。
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
directors=[[NSMutableArray alloc]init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Book" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
NSLog(@"1");
[directors addObject:[info valueForKey:@"director"]];
NSLog(@"Name: %@", info);
}
NSLog(@"%@",directors);
正在打印 'NSLog(@"1"); 以检查循环工作了多少次,但是
并得到这样的输出
2014-06-15 15:40:03.665 test[12489:1303] Name: <Book: 0x8c50580> (entity: Book; id: 0x8c66a60 <x-coredata://EEF91D97-C982-40E1-A898-9E646D206B39/Book/p1> ; data: <fault>)
2014-06-15 15:40:03.665 test[12489:1303] 1
2014-06-15 15:40:03.666 test[12489:1303] Name: <Book: 0x8c690d0> (entity: Book; id: 0x8c68420 <x-coredata://EEF91D97-C982-40E1-A898-9E646D206B39/Book/p2> ; data: <fault>)
2014-06-15 15:40:03.666 test[12489:1303] 1
2014-06-15 15:40:03.667 test[12489:1303] Name: <Book: 0x8f9c8d0> (entity: Book; id: 0x8d89870 <x-coredata://EEF91D97-C982-40E1-A898-9E646D206B39/Book/p3> ; data: {
actors = "Hazel Crowney, Kiran Kumar, Shahbaaz Khan, Usha Bachani, Mohammed Iqbal Khan, Alka Verma";
censor = U;
director = "Arshad Yusuf Pathan";
global = N;
lang = Hindi;
length = "";
rating = 0;
releasedate = "13th Jun 2014";
synopsis = "The story of a racecar driver who loses his eyesight. While he suffers a setback, he learns about the true value of the relationships in his life. Through the course of the film, the hero realizes the";
title = Unforgettable;
trailer = "";
type = MT;
url = "http://cnt.in.bookmyshow.com/Events/Mobile/ET00022474.jpg?dtm=15614303";
})
当前代码
【问题讨论】:
标签: ios objective-c json core-data