首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI错误:“查看”.Type‘不可转换为'(String,Glyph) -> "View’

SwiftUI错误:“查看”.Type‘不可转换为'(String,Glyph) -> "View’
EN

Stack Overflow用户
提问于 2020-03-19 15:40:59
回答 1查看 267关注 0票数 1

第一个SwiftUI项目:)我从顶级视图中的列表中获得了一个NavLink,该视图调用了一个详细视图。在我需要将Struct的@State添加到detail视图之前,它一直在工作,现在调用参数列表与编译器所期望的不匹配:

代码语言:javascript
复制
    ZStack {
        VStack {
            NavigationView {
                    List(gList) { gard in
                        NavigationLink(destination:
                            SelectGlyph(catList: gard.category,
                                        firstGlyph: gard.ex1,
                                        descr: gard.title,
                                        selectedGlyph: Glyph() )
                        {

细节视图从以下几个方面开始:

代码语言:javascript
复制
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 {

字形结构被定义为:

代码语言:javascript
复制
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目标中:

代码语言:javascript
复制
'SelectGlyph.Type' is not convertible to '(String, String, String, Glyph) -> SelectGlyph'

我尝试过调用列表中的几个变体,并尝试删除SelectGlyph中的@State包装器,所有这些都没有效果。任何帮助都将不胜感激!如果我知道如何在View中指定局部变量,而不将它作为调用中必需的参数(我相信我在这里遗漏了什么),那就太好了!

作为对Asperi的响应,我尝试了SelectGlyph视图的显式初始化程序,但仍然在相同的位置得到相同的错误:

代码语言:javascript
复制
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

代码语言:javascript
复制
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() {
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-19 15:45:18

它就在这里

NavigationLink(destination: SelectGlyph(catList: gard.category, firstGlyph: gard.ex1, descr: gard.title, selectedGlyph: Glyph() )

这里需要显式初始化器,如

代码语言:javascript
复制
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中的')‘。

代码语言:javascript
复制
NavigationLink(destination:
    SelectGlyph(catList: gard.category,
                firstGlyph: gard.ex1,
                descr: gard.title,
                selectedGlyph: Glyph() )) // << here !!
{
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60760497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档