第一个SwiftUI项目:)我从顶级视图中的列表中获得了一个NavLink,该视图调用了一个详细视图。在我需要将Struct的@State添加到detail视图之前,它一直在工作,现在调用参数列表与编译器所期望的不匹配:
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包装器,所有这些都没有效果。任何帮助都将不胜感激!如果我知道如何在View中指定局部变量,而不将它作为调用中必需的参数(我相信我在这里遗漏了什么),那就太好了!
作为对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() {发布于 2020-03-19 15:45:18
它就在这里
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 !!
{https://stackoverflow.com/questions/60760497
复制相似问题