Xcode Beta 6“没有更多上下文的表达类型是模棱两可的"导航链接 [英] Xcode Beta 6 "Type of expression is ambiguous without more context" navigationlink

查看:28
本文介绍了Xcode Beta 6“没有更多上下文的表达类型是模棱两可的"导航链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从今天早些时候更新到 Xcode Beta 6 以来,我的应用程序将不再构建,它在 Beta 5 及更早版本中运行良好.

Since updating to Xcode Beta 6 earlier today my application will no longer build, it was working fine in Beta 5 and earlier.

这是带有错误消息的文件中的代码,尽管我目前知道这并不一定意味着这就是错误的实际所在.

This is the code from the file with the error message, although I'm aware at the moment this doesn't necessarily mean this is where the error actually lies.

import SwiftUI

struct JobView_Table : View {

    @ObservedObject var jobList: JobDetailViewModel = JobDetailViewModel()

    var body: some View {

        NavigationView {

            List {
                ForEach($jobList.jobDetails) { job in
                    NavigationLink(destination: JobDetailHost(jobDetails: job)) { // ERROR: "Type of expression is ambiguous without more context"
                        JobView_List(jobDetails: job)
                    }
                }
            }

            .navigationBarTitle(Text("My Jobs"))
            .onAppear(perform: fetchData)
            .onAppear(perform: {
                print("Hello!")
            })

        }
    }

    private func fetchData() {
        return(jobList.updateDetails())
    }
}

包含数据的结构体正确地符合以下协议.

The struct containing the data conforms correctly to the following protocols.

struct JobDetails: Codable, Identifiable, Equatable, Hashable { 
    ...

    ...
}

这是向JobView_Table提供数据的类.

import Foundation
import UIKit
import Combine

class JobDetailViewModel: ObservableObject, Identifiable {

    @Published var jobDetails: [JobDetails] = []

    func updateDetails() {
        self.jobDetails = DataManager().fetchJobList()
    }

}

最后是通过 NavigationLink 链接到的目标视图.

And finally the target view which is linked to via the NavigationLink.

struct JobDetailHost: View {

    @Environment(\.editMode) var mode
    @Binding var jobDetails: JobDetails


    var body: some View {

        VStack {
            JobDetailView(jobDetails: jobDetails)
        }
        .navigationBarItems(trailing: EditButton())
    }

}

我注意到其他一些人似乎也有类似的问题,即在下面列出的两个问题中,但目前探索这些问题的答案对我没有帮助.SwiftUI Xcode 11 测试版5/6:表达类型不明确,没有更多上下文

I notice that some others seem to be having similar problems, i.e. in the two questions listed below, but exploring the answers in these questions is not helping me at this moment. SwiftUI Xcode 11 beta 5 / 6: Type of expression is ambiguous without more context

SwiftUI:为什么 ForEach($strings) (text: Binding) 未构建?

我已尝试实施 Fabian 的建议,这已经消除了错误,但是列表中没有填充任何内容.

I have tried implementing the suggestion from Fabian, this has got rid of the error, however no content is being populated in the list.

这是调整后的 List 代码,它编译成功,但在运行应用程序时未填充列表.

This is the adjusted List code, which compiles successfully but when running the application the list is not populated.

List {
    ForEach(jobList.jobDetails.indexed(), id: \.1.id) { (index, job) in
            NavigationLink(destination: JobDetailHost(jobDetails: self.$jobList.jobDetails[index])) {
                        Text(job.jobName)
                    }
     }
}

下面没有使用ForEach,丢弃了NavigationLink,还是不行.

The following does not use the ForEach and discards the NavigationLink, and still doesn't work.

List(jobList.jobDetails.indexed(), id: \.1.id) { (index, job) in
                Text(job.jobName)
            }

推荐答案

我会引用 macOS Catalina 10.15 Beta 6 发行说明:

绑定结构对集合的条件一致性协议被删除.(51624798)

The Binding structure’s conditional conformance to the Collection protocol is removed. (51624798)

如果您有如下代码:

struct LandmarkList: View {
    @Binding var landmark: [Landmark]

    var body: some View {
        List(landmarks) { landmark in
            Toggle(landmark.value.name, isOn: landmark[\.isFavorite])
        }
    }
}

定义以下集合类型:

struct IndexedCollection<Base: RandomAccessCollection>: RandomAccessCollection {
    typealias Index = Base.Index
    typealias Element = (index: Index, element: Base.Element)

    let base: Base

    var startIndex: Index { base.startIndex }

    var endIndex: Index { base.endIndex }

    func index(after i: Index) -> Index {
        base.index(after: i)
    }

    func index(before i: Index) -> Index {
        base.index(before: i)
    }

    func index(_ i: Index, offsetBy distance: Int) -> Index {
        base.index(i, offsetBy: distance)
    }

    subscript(position: Index) -> Element {
        (index: position, element: base[position])
    }
}

extension RandomAccessCollection {
    func indexed() -> IndexedCollection<Self> {
        IndexedCollection(base: self)
    }
}

然后,将您的代码更新为:

Then, update your code to:

struct LandmarkList: View {
    @Binding var landmarks: [Landmark]

    var body: some View { // Does often give error on id: \.1.id
        List(landmarks.indexed(), id: \.1.id) { (index, landmark) in
            Toggle(landmark.name, isOn: self.$landmarks[index].isFavorite)
        }
    }
}

你的代码还需要一个 Binding<[JobDetails]>$jobList.jobDetails,但是 Binding<[JobDetails]>不再符合 Collection 协议.

Your code also takes a Binding<[JobDetails]> with $jobList.jobDetails, but Binding<[JobDetails]> does not conform to the Collection protocol anymore.

但是请注意上面的解决方案,我遇到了无法识别 \.1.id 的情况,因为编译器不理解 \.1 指的是元组中的第二个元素 IndexedCollection 定义,但可能我用错了.但是可以重写它,这使它起作用.

Note to the solution above however, that I got cases where \.1.id was not recognized because the compiler didn't understand \.1 is referring to the second element in the tuple IndexedCollection defines, but it's possible that I used it wrong. It's possible to rewrite it however which makes it work.

struct AnotherIndexedView_NeedsEnv: View {
    @EnvironmentObject var modalManager: ModalManager

    var body: some View {
        ZStack {
            SwiftUI.ForEach(modalManager.modals.indexed()) { m in
                ModalView(currentModal: self.$modalManager.modals[m.index]).environmentObject(self.modalManager)
            }
        }.onAppear(perform: {self.modalManager.fetchContent()})
    }
}

这篇关于Xcode Beta 6“没有更多上下文的表达类型是模棱两可的"导航链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆