【问题标题】:Cannot convert value of type 'NSSet?' to expected argument type 'Range<Int>' (using CoreData)无法转换“NSSet”类型的值?到预期的参数类型'Range<Int>'(使用CoreData)
【发布时间】:2020-11-23 18:09:29
【问题描述】:

使用 CoreData 我不知道如何解决这个问题:

错误:Cannot convert value of type 'NSSet?' to expected argument type 'Range&lt;Int&gt;'

private func displayTopics(subject: Subject) -> some View {
    NavigationView {
        Form {
            List {
                ForEach(subject.topics) { topic in
                    NavigationLink(
                        destination: Text("Destination"),
                        label: {
                            Text("Navigate")
                    })
                }
            }
        }
    }

我该怎么办?

【问题讨论】:

    标签: core-data swiftui


    【解决方案1】:

    ForEach 看不懂NSSet,得转成数组:

    if subject.topics != nil {
        ForEach(Array(subject.topics! as Set), id: \.self) { topic in
            NavigationLink(
                destination: Text("Destination"),
                label: {
                    Text("Navigate")
            })
        }
    }
    

    【讨论】:

    • 如何访问“主题”的属性“标题”? ...“NSObject”类型的值没有成员“title”
    • 你没有展示 topics 里面的东西,这是你的模型......这是不同的问题。
    • 我们应该如何进行?
    猜你喜欢
    • 1970-01-01
    • 2020-11-11
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多