SwiftUI error: "View".Type ' is not convertible to '(String, String, String, Glyph) -> "View'
SwiftUI error: "View".Type ' is not convertible to '(String, String, String, Glyph) -> "View'
第一个 SwiftUI 项目:)
我在调用详细视图的顶级视图中的列表中有一个 NavLink。
它一直在工作,直到我需要将结构的 @State var 添加到详细视图中,现在调用参数列表与编译器期望的不匹配:
ZStack {
VStack {
NavigationView {
List(gList) { gard in
NavigationLink(destination:
SelectGlyph(catList: gard.category,
firstGlyph: gard.ex1,
descr: gard.title,
selectedGlyph: Glyph() )
{
详细视图开始:
struct SelectGlyph: View {
var catList: String
var firstGlyph: String
var descr: String
@State var selectedGlyph:Glyph = Glyph()
var body: some View {
ZStack{
VStack() {
VStack {
VStack {
VStack {
字形结构定义为:
struct Glyph: Codable, Identifiable, Equatable {
var id:String {
return Gardiner
}
var Hieroglyph: String
var Gardiner: String
let Unicode: String
let Description: String
let Transliteration: String
let Phonetic: String
let Notes: String
init() {
self.Hieroglyph = ""
self.Gardiner = ""
self.Unicode = ""
self.Description = ""
self.Transliteration = ""
self.Phonetic = ""
self.Notes = ""
}
init(Hiero:String, Gard:String, Uni:String, Desc:String, trans:String, phon:String, notes:String) {
self.Hieroglyph = Hiero
self.Gardiner = Gard
self.Unicode = Uni
self.Description = Desc
self.Transliteration = trans
self.Phonetic = phon
self.Notes = notes
}
}
错误在顶层 ContentView navLink 目标:
'SelectGlyph.Type' is not convertible to '(String, String, String, Glyph) -> SelectGlyph'
我在调用列表中尝试了几种变体,并尝试删除 SelectGlyph 中的 @State 包装器,但都没有效果。任何帮助,将不胜感激!如果我知道如何在视图中指定局部变量而不是调用中的必需参数,那就太好了(我确定我在这里遗漏了一些东西!)
作为对 Asperi 的回应,我尝试了 SelectGlyph 视图的显式初始化程序,但仍然在同一个地方遇到同样的错误:
struct ContentView: View {
var gList: [GardinerList] = gardinerTest
var body: some View {
UINavigationBar.appearance().backgroundColor = .yellow
return
ZStack {
VStack {
NavigationView {
List(gList) { gard in
NavigationLink(destination:
SelectGlyph(catList: gard.category, <- Error Here
firstGlyph: gard.ex1,
descr: gard.title,
selectedGlyph: Glyph() )
{
Text(gard.category)
.fontWeight(.bold)
.foregroundColor(Color.green)
SelectGlyph 视图现已修改为具有显式 init()。我尝试在 init() 中将 selectedGlyph 指定为 _selectedGlyph 和 self._selectedGlyph
struct SelectGlyph: View {
var catList: String
var firstGlyph: String
var descr: String
@State private var selectedGlyph:Glyph = Glyph()
init(catList: String, firstGlyph: String, descr: String, selectedGlyph:Glyph) {
self.catList = catList
self.firstGlyph = firstGlyph
self.descr = descr
self._selectedGlyph = State<Glyph>(initialValue: selectedGlyph)
}
var body: some View {
ZStack{
VStack() {
就在这里
NavigationLink(destination:
SelectGlyph(catList: gard.category,
firstGlyph: gard.ex1,
descr: gard.title,
selectedGlyph: Glyph() )
你需要显式初始化,比如
struct SelectGlyph: View {
var catList: String
var firstGlyph: String
var descr: String
@State private var selectedGlyph:Glyph // << always keep @State private !!
init(catList: String, firstGlyph: String, descr: String, selectedGlyph:Glyph) {
self.catList = catList
self.firstGlyph = firstGlyph
self.descr = descr
_selectedGlyph = State<Glyph>(initialValue: selectedGlyph)
}
更新:再来一个。如果不是复制粘贴拼写错误,那么你错过了 NavigationLink
中的 ')'
NavigationLink(destination:
SelectGlyph(catList: gard.category,
firstGlyph: gard.ex1,
descr: gard.title,
selectedGlyph: Glyph() )) // << here !!
{
第一个 SwiftUI 项目:) 我在调用详细视图的顶级视图中的列表中有一个 NavLink。 它一直在工作,直到我需要将结构的 @State var 添加到详细视图中,现在调用参数列表与编译器期望的不匹配:
ZStack {
VStack {
NavigationView {
List(gList) { gard in
NavigationLink(destination:
SelectGlyph(catList: gard.category,
firstGlyph: gard.ex1,
descr: gard.title,
selectedGlyph: Glyph() )
{
详细视图开始:
struct SelectGlyph: View {
var catList: String
var firstGlyph: String
var descr: String
@State var selectedGlyph:Glyph = Glyph()
var body: some View {
ZStack{
VStack() {
VStack {
VStack {
VStack {
字形结构定义为:
struct Glyph: Codable, Identifiable, Equatable {
var id:String {
return Gardiner
}
var Hieroglyph: String
var Gardiner: String
let Unicode: String
let Description: String
let Transliteration: String
let Phonetic: String
let Notes: String
init() {
self.Hieroglyph = ""
self.Gardiner = ""
self.Unicode = ""
self.Description = ""
self.Transliteration = ""
self.Phonetic = ""
self.Notes = ""
}
init(Hiero:String, Gard:String, Uni:String, Desc:String, trans:String, phon:String, notes:String) {
self.Hieroglyph = Hiero
self.Gardiner = Gard
self.Unicode = Uni
self.Description = Desc
self.Transliteration = trans
self.Phonetic = phon
self.Notes = notes
}
}
错误在顶层 ContentView navLink 目标:
'SelectGlyph.Type' is not convertible to '(String, String, String, Glyph) -> SelectGlyph'
我在调用列表中尝试了几种变体,并尝试删除 SelectGlyph 中的 @State 包装器,但都没有效果。任何帮助,将不胜感激!如果我知道如何在视图中指定局部变量而不是调用中的必需参数,那就太好了(我确定我在这里遗漏了一些东西!)
作为对 Asperi 的回应,我尝试了 SelectGlyph 视图的显式初始化程序,但仍然在同一个地方遇到同样的错误:
struct ContentView: View {
var gList: [GardinerList] = gardinerTest
var body: some View {
UINavigationBar.appearance().backgroundColor = .yellow
return
ZStack {
VStack {
NavigationView {
List(gList) { gard in
NavigationLink(destination:
SelectGlyph(catList: gard.category, <- Error Here
firstGlyph: gard.ex1,
descr: gard.title,
selectedGlyph: Glyph() )
{
Text(gard.category)
.fontWeight(.bold)
.foregroundColor(Color.green)
SelectGlyph 视图现已修改为具有显式 init()。我尝试在 init() 中将 selectedGlyph 指定为 _selectedGlyph 和 self._selectedGlyph
struct SelectGlyph: View {
var catList: String
var firstGlyph: String
var descr: String
@State private var selectedGlyph:Glyph = Glyph()
init(catList: String, firstGlyph: String, descr: String, selectedGlyph:Glyph) {
self.catList = catList
self.firstGlyph = firstGlyph
self.descr = descr
self._selectedGlyph = State<Glyph>(initialValue: selectedGlyph)
}
var body: some View {
ZStack{
VStack() {
就在这里
NavigationLink(destination: SelectGlyph(catList: gard.category, firstGlyph: gard.ex1, descr: gard.title, selectedGlyph: Glyph() )
你需要显式初始化,比如
struct SelectGlyph: View {
var catList: String
var firstGlyph: String
var descr: String
@State private var selectedGlyph:Glyph // << always keep @State private !!
init(catList: String, firstGlyph: String, descr: String, selectedGlyph:Glyph) {
self.catList = catList
self.firstGlyph = firstGlyph
self.descr = descr
_selectedGlyph = State<Glyph>(initialValue: selectedGlyph)
}
更新:再来一个。如果不是复制粘贴拼写错误,那么你错过了 NavigationLink
NavigationLink(destination:
SelectGlyph(catList: gard.category,
firstGlyph: gard.ex1,
descr: gard.title,
selectedGlyph: Glyph() )) // << here !!
{