gpt4 book ai didi

ios - 如何从核心数据表 ios 中检索数据?

转载 作者:行者123 更新时间:2023-11-29 12:43:39 24 4
gpt4 key购买 nike

嗨,我在我的测试项目中使用了核心数据。我能够将 json 中的内容添加到核心数据实体“书籍”。但是当我尝试检索数据时,我只得到最后一个“数据”(总共 37 个数据)?我创建了一个可变数组“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";
})

当前代码 enter image description here

enter image description here

enter image description here

最佳答案

正如我从日志中看到的那样,除了最后一个之外,您从 CoreData 收到的所有托管对象都是错误的。这意味着这些对象是表示实体(书籍)的适当类的实例,但它们未被初始化。如果您尝试访问接收记录的某些字段,它们将自动初始化。我想对您的代码进行以下修改应该可行:

for (Book *info in fetchedObjects) {
NSLog(@"1");
[directors addObject:info.director];
NSLog(@"Name: %@", info);

}
NSLog(@"%@",directors);

这里的Book是一个继承自NSManagedObject的类,表示名为Book的实体。您可以通过突出显示 CoreData 模型中的实体并从 Editor 菜单中选择 Create NSManagedObject subclass 项来为任何实体生成此类。

有关故障的更多信息:Core Data Programming Gudie

关于ios - 如何从核心数据表 ios 中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24228621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com