这里是全新的编码器,也是第一次使用。如果这对大多数人来说太容易了,我深表歉意。我在使用naviationLink从上下文视图切换到详细视图时遇到问题。我想展示一排吉他,然后展示从表中选择的吉他的详细视图。
以下是一些详细信息。我有一个Guitars.swift文件,其中包含吉他列表。 (见下文)
import Foundation
struct Guitars: Identifiable,Hashable {
let id: Int
let name,description,imageName: String
}
let guitar: [Guitars] = [
.init (id: 1,name: "Classical",description: "This is a nylon string guitar that is acoustic and made if cedar or spruce wood",imageName: "classical"),.init (id: 2,name: "Acoustic",description: "The acoustic guitar is a steel string instrument that is traditionally played as an accompanying part of a musical performance.",imageName: "acoustic"),.init (id: 3,name: "Electric",description: "This is a steel string guitar that is electic with pickups and made to be played through amplifiers",imageName: "electric_guitar-1"),.init (id: 4,name: "Banjo",description: "This is a steel string guitar that has five strings. It's usually played in country and bluegrass music",imageName: "banjo"),.init (id: 5,name: "Bass",description: "This is a steel string guitar that has four strings. It's usually played in all types of bands and provides the bass line for the music",imageName: "bass")
]
我有一个GuitarDetails文件,其中包含定义的属性和结构。 (见下文)
struct GuitarDetails: View {
var selectedGuitar: Guitars
var body: some View {
VStack{
Image(selectedGuitar.name)
.resizable()
.frame(width:300,height: 300)
VStack {
Spacer()
}
}
}
}
struct GuitarDetails_Previews: PreviewProvider {
static var previews: some View {
GuitarDetails(selectedGuitar: <#T##Guitars#>)
最后,我有一个ContentView文件,我在其中创建了NavigationLink,以从内容视图移动到详细信息视图,但是无法正常工作。有人可以帮我吗?这是内容查看代码。
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List (Guitars) { guitar in
NavigationLink(destination: GuitarDetails(selectedGuitar: guitar) {
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
我得到的错误是:“'GuitarDetails.Type'无法转换为'((Guitars)-> GuitarDetails'