首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用UIHostingController实例化CoreData?

如何用UIHostingController实例化CoreData?
EN

Stack Overflow用户
提问于 2020-08-20 11:28:46
回答 2查看 176关注 0票数 3

作为一个长期的Obj(自iPhone OS 2以来),我决定SwiftUI是使用Swift!¯_(ツ)_/的推动者,因此,我仍在试图理解一种语言如何能够如此类型模糊-代码模糊,以及编译器中的类型学!

尝试获取Swift/Swift use实例化UIHostingController<>,以便与CoreData一起使用

代码语言:javascript
复制
class MyCoreDataHostingController : UIHostingController<MyCoreDataView> {

    required init?(coder: NSCoder) {//Instantiate From Storyboard
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let contentView = MyCoreDataView().environment(\.managedObjectContext, context)
        super.init(coder: coder, rootView: contentView)
        //Cannot convert value of type 'some View' to expected argument type 'MyCoreDataView'
    }
}

struct MyCoreDataView: View {
    var body: some View {
        Text("Core Data View")
    }
}

我怎样才能强迫Swift推断出正确的/合适的类型?

我已经尝试过了。

代码语言:javascript
复制
5 let contentView = MyCoreDataView()

编译/运行,但不包括CoreData。

代码语言:javascript
复制
6 super.init(coder: coder, rootView: contentView as! MyCoreDataView)

编译,但在实例化时崩溃。

'SwiftUI.ModifiedContent>‘(.)类型的

不能转换值“MyHostingController.MyCoreDataView”(.)。

..。还是根本不可能?(尚未)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-20 12:11:02

这是最快的解决方案--只要使用AnyView类型的橡皮擦就行了.

注意:任何视图修饰符都会创建(通常可以创建)不同类型的视图,这就是为什么编译器在您的情况下抱怨-类型是不同的。

代码语言:javascript
复制
class MyCoreDataHostingController : UIHostingController<AnyView> {

    required init?(coder: NSCoder) {//Instantiate From Storyboard
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let contentView = MyCoreDataView().environment(\.managedObjectContext, context)
        super.init(coder: coder, rootView: AnyView(contentView))
    }
}
票数 1
EN

Stack Overflow用户

发布于 2020-08-21 10:28:51

由OOPer在Apple论坛上发布:

正如您所看到的,SwiftUI视图的修饰符返回一些不透明的类型--一些视图,程序员没有看到它,但是Swift编译器声称每个类型都匹配.

一个可能的解决方案是创建一个包装器视图:

代码语言:javascript
复制
class MyCoreDataHostingController : UIHostingController<MyCoreDataViewEnvironmentWrapper> {
    required init?(coder: NSCoder) {//Instantiate From Storyboard
        let contentView = MyCoreDataViewEnvironmentWrapper()
        super.init(coder: coder, rootView: contentView)
    }
}
struct MyCoreDataViewEnvironmentWrapper: View {
    private let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    var body: some View {
        MyCoreDataView().environment(\.managedObjectContext, context)
    }
}

或者您可以选择AnyView作为包装:

代码语言:javascript
复制
class MyCoreDataHostingController : UIHostingController<AnyView> {
    required init?(coder: NSCoder) {//Instantiate From Storyboard
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let contentView = AnyView(MyCoreDataView().environment(\.managedObjectContext, context))
        super.init(coder: coder, rootView: contentView)
    }
}

我不确定这是否适用于你的案子,但请你试试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63504070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档