首页 > 解决方案 > 无法在共享扩展中调用 CoreData 实体

问题描述

我有一个名为Item.

import Foundation
import CoreData

@objc(Item)
public class Item: NSManagedObject, Identifiable {

...
}

我想调用Item共享扩展,例如:

import UIKit
import Social
import CoreServices
import CoreData

class ShareViewController: SLComposeServiceViewController {
    
    var item: Item?  // <-- Use of undeclared type 'Item'
}

我已将主应用程序和共享扩展设置为相同的应用程序组和相同的容器。为什么代码可以调用Entity?

但我的说Use of undeclared type 'Item'。如何在共享扩展中调用 CoreData?

更新

我关注@vadian 的评论,并且Item可以在共享扩展中使用。感谢@vadian 提醒我!

修改目标成员身份后,当我运行共享扩展时,如下所示:

override func didSelectPost() {
    print(Item.some method~)
}

错误说ShareExtension*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: <null> is not a valid NSFetchRequest.'

但是该方法print(Item.some method~)可以在主应用程序中正确使用。

发生错误的原因是什么?谢谢你的帮助。

标签: iosswiftcore-dataios8-share-extension

解决方案


推荐阅读