Ios NSManagedObject在cellForItemAt indexPath中变为零
我使用水平滚动的collectionView。在viewdidload中,我获取coredata对象Ios NSManagedObject在cellForItemAt indexPath中变为零,ios,swift,core-data,Ios,Swift,Core Data,我使用水平滚动的collectionView。在viewdidload中,我获取coredata对象 var listOfArticles = [Article]() override func viewDidLoad() { super.viewDidLoad() listOfArticles = CoreDataManager().fetchArticleWithCategory(category: "Entertainment", limit: 10)
var listOfArticles = [Article]()
override func viewDidLoad() {
super.viewDidLoad()
listOfArticles = CoreDataManager().fetchArticleWithCategory(category: "Entertainment", limit: 10)!
}
当我在viewdidload中打印项目列表时
for newData in listOfArticles {
print(newData.article_id)
}
输出:-
在indexath.item==0时,我能够获取Article
objet,但indexath.item==1Article
objet变为空
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell :NewsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! NewsCollectionViewCell
var articles = listOfArticles[indexPath.item]
cell.currentArticleToShow = articles
if let newVar = articles.articleToSectionRel?.allObjects {
cell.currentSections = newVar as! [Section]
}
}
当我尝试在indexath.item==0处打印采购订单文章时
输出:
为什么在我将数据提取到viewdidload时,Articles对象在object indexPath.item==1处变为null?。提前谢谢 这看起来是正确的。你在索引1的文章是错误的;它还没有被取出来。一旦访问某个属性,核心数据就会将获取转换为实际对象。请尝试
po articles[“article_id”]
更新问题..我在cellForItemAt indexPath的NSMangeObject中没有看到空的objectentity:在indexPath.item=1处的cellForItemAt indexPath中。您正在创建新对象CoreDataManager()
希望您的上下文是共享的或全局的,否则您将无法检查我的问题
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell :NewsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! NewsCollectionViewCell
var articles = listOfArticles[indexPath.item]
cell.currentArticleToShow = articles
if let newVar = articles.articleToSectionRel?.allObjects {
cell.currentSections = newVar as! [Section]
}
}
<Article: 0x60800009f2c0> (entity: Article; id: 0xd000000000100024 <x-coredata://BA36E40C-6783-4CBF-8C2F-F596ABF1CD56/Article/p4> ; data: {
articleToSectionRel = "<relationship fault: 0x600000426a00 'articleToSectionRel'>";
"article_id" = 1599845;
"book_mark" = 0;
category = Entertainment;
"image_url" = "http://images.newindianexpress.com/uploads/user/imagelibrary/2017/4/20/original/KatyPerry-AP1.png";
"modiffied_date" = "2017-05-01 16:52:00";
"sub_category" = English;
summary = "http://images.newindianexpress.com/uploads/user/imagelibrary/2017/4/20/original/KatyPerry-AP1.png";
title = "Katy Perry courts controversy\U00a0comparing her black hair to Barack Obama";
topStorie = 0;
})
<Article: 0x608000280370> (entity: <null>; id: 0xd000000000180024 <x-coredata://BA36E40C-6783-4CBF-8C2F-F596ABF1CD56/Article/p6> ; data: <fault>)
listOfArticles = CoreDataManager().fetchArticleWithCategory(category: "Entertainment", limit: 10)!