gpt4 book ai didi

xcode - 如果@EnvironmentObject,如何创建通用?

转载 作者:行者123 更新时间:2023-12-01 21:36:03 24 4
gpt4 key购买 nike

我最近发现需要编写一个 Class 的 Mock,因为它会导致 SwiftUI preview 无法正常工作。不幸的是,我得到了错误:

Property type 'T' does not match that of the 'wrappedValue' property of its wrapper type 'EnvironmentObject'

在 View 结构中:

struct ContentView<T>: View {
@EnvironmentObject var mockFoobar: T
...
}

还有错误:

Type of expression is ambiguous without more context

对于预览结构:

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let mockFoobar: MockFoobar = MockFoobar()
return ContentView<MockFoobar>()
.environmentObject(mockFoobar)
}
}

MockFoobar 类是:

class MockFoobar: ObservableObject {
...
}

正如用户@Asperi 提供的那样,按照建议测试了以下内容:

class Foobar: ObservableObject {
@Published var param: Bool = false
func start() {
self.param = true
}
}

struct MyFoobarView<T: ObservableObject>: View {
@EnvironmentObject var foobar: T

var body: some View {
VStack {
Text("Hello Foobar")
}
.onAppear {
self.foobar.start()
}
}
}

struct MyFoobarView_Previews: PreviewProvider {
static var previews: some View {
let foobar: Foobar = Foobar()
return MyFoobarView()
.environmentObject(foobar)
}
}

但我收到以下错误(第一个在 .onAppear 中,第二个在 PreviewProvider 中):

Cannot call value of non-function type 'Binding<Subject>'

Generic parameter 'T' could not be inferred

最佳答案

EnvironmentObject必须是ObservableObject,所以这里修复

struct ContentView<T: ObservableObject>: View {
@EnvironmentObject var mockFoobar: T

// .. other code here

更新:添加了引入模型协议(protocol)的演示

protocol Foobaring {
var param: Bool { get set }
func start()
}

class Foobar: ObservableObject, Foobaring {
@Published var param: Bool = false
func start() {
self.param = true
}
}

struct MyFoobarView<T: ObservableObject & Foobaring>: View {
@EnvironmentObject var foobar: T

var body: some View {
VStack {
Text("Hello Foobar")
}
.onAppear {
self.foobar.start()
}
}
}

struct MyFoobarView_Previews: PreviewProvider {
static var previews: some View {
let foobar: Foobar = Foobar()
return MyFoobarView<Foobar>()
.environmentObject(foobar)
}
}

关于xcode - 如果@EnvironmentObject,如何创建通用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61972651/

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