Xcode Beta 6 "Type of expression is ambiguous without more context"导航链接



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

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

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())
}
}

包含数据的结构正确符合以下协议。

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链接到的目标视图。

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 beta 5/6:表达式类型不明确,没有更多上下文

SwiftUI:为什么 ForEach($strings((文本:Binding(不构建?

编辑:

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

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

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

以下不使用ForEach并丢弃NavigationLink,仍然不起作用。

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

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

绑定结构与集合的条件一致性 协议被删除。(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)
}
}

然后,将代码更新为:

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)
}
}
}

您的代码也与$jobList.jobDetailsBinding<[JobDetails]>,但Binding<[JobDetails]>不再符合集合协议。

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

使用索引集合的示例

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()})
}
}

相关内容

最新更新