第一个 SwiftUI 项目 :) 我有一个来自顶层视图列表的 NavLink,它调用了一个详细视图。它一直在工作,直到我需要将 Struct 的 @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() {