gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:50:42 25 4
gpt4 key购买 nike

自从今天早些时候更新到 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())
}
}

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

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

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: Type of expression is ambiguous without more context

SwiftUI: Why does ForEach($strings) (text: Binding) not build?

编辑:

我已经尝试实现 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 Release Notes :

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

If you have code such as the following:

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

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

Define the following collection type:

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 协议(protocol)。

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

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

关于swift - Xcode Beta 6 "Type of expression is ambiguous without more context"导航链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57580748/

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