【问题标题】:ForEach with @State property带有@State 属性的 ForEach
【发布时间】:2020-03-26 10:33:20
【问题描述】:

我有一个@State 属性(在示例中为@State var parent: Parent),我想在SwiftUI 的ForEach 循环中迭代一个数组属性。当我这样做时,这很好用

ForEach(parent.children, id: \.self) { child in
    ChildView(child: child)
}

虽然我相信在对correctly pass it to the child view 的调用中添加$。但是,我收到Unable to infer complex closure return type; add explicit type to disambiguate 的错误。

解决这个问题的正确方法是什么?为什么这段代码不正确?

最少的复制代码

import UIKit
import SwiftUI

func setup() -> Parent {
    let child = Child(name: "foo")
    let parent = Parent(name: "bar")
    parent.children = [child]
    return parent
}

class Parent {
    var name: String
    var children: [Child] = []

    init(name: String) {
        self.name = name
    }
}

class Child: Hashable {
    static func == (lhs: Child, rhs: Child) -> Bool {
        lhs.name == rhs.name
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(self.name)
    }

    var name: String

    init(name: String) {
        self.name = name
    }
}

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        let parent = setup()
        let parentView = ParentView(parent: parent)

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: parentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
}

struct ChildView: View {
    @State var child: Child

    var body: some View {
        VStack {
            Text("Child “\(child.name)”")
        }
    }
}

struct ParentView: View {
    @State var parent: Parent

    var body: some View {
        VStack {
            Text("Parent “\(parent.name)”")
            // ForEach($parent.children, id: \.self) { child in
            // Yields error:
            // Unable to infer complex closure return type; add explicit type to disambiguate
            ForEach(parent.children, id: \.self) { child in
                ChildView(child: child)
            }
        }
    }
}

struct ParentView_Previews: PreviewProvider {
    static var previews: some View {
        let parent = setup()
        return ParentView(parent: parent)
    }
}

【问题讨论】:

  • 仅当您尝试传入 Binding 或 Binding 时才使用美元符号,据我所知您不是。唯一一次使用 _ 或 $ 是使用 @propertyWrappers 的对象。我不明白你为什么需要在这里。

标签: swift foreach swiftui


【解决方案1】:

我不知道为什么你需要美元符号,但我会在下面更改此代码

class Parent: ObservableObject {
    var name: String
    @Published
    var children: [Child] = []

    init(name: String) {
        self.name = name
    }
}

class Child: Hashable {
    static func == (lhs: Child, rhs: Child) -> Bool {
        lhs.name == rhs.name
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(self.name)
    }

    var name: String

    init(name: String) {
        self.name = name
    }
}

struct ChildView: View {
    @State var child: Child

    var body: some View {
        VStack {
            Text("Child “\(child.name)”")
        }
    }
}

struct ParentView: View {
    @ObservedObject
    var parent: Parent

    var body: some View {
        VStack {
            Text("Parent “\(parent.name)”")
            // ForEach($parent.children, id: \.self) { child in
            // Yields error:
            // Unable to infer complex closure return type; add explicit type to disambiguate
            ForEach(parent.children, id: \.self) { child in
                ChildView(child: child)
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    相关资源
    最近更新 更多