【问题标题】:Error messges: "Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type"错误消息:“函数声明了一个不透明的返回类型,但它的主体中没有返回语句来推断基础类型”
【发布时间】:2020-08-29 01:37:15
【问题描述】:

我正在制作一个简单的琐事应用程序,您最终会在其中选择一个问题的两个答案。到目前为止,我有一些变量和数组来保存我关于什么是正确的和什么是错误的信息。我有一些错误我无法识别任何帮助?我正在使用 SwiftUI,但我真的很新。

问题是我的 VStack 给了我这个消息“'VStack'初始化程序的结果未使用”

我的视图给了我这个错误:“函数声明了一个不透明的返回类型,但它的主体中没有返回语句来推断基础类型”

这是我的代码:

import SwiftUI

struct ContentView: View {
    
    @State var currentNum = 0
    var person = ["Michael Jackson","Elton John","Prince"]
    var dead = [1,0,1]
    // 1 = yes 0 = no
    @State var correct = true
    
    var body: some View {
        VStack{
        Text(person[currentNum])
            
            Button(action: {
                
            }, label: {
                Text("Dead")
            })
            
            
            
        }
        
        func checkDead() {
            if dead[currentNum] == 1{
                return correct = true
            }
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

谢谢 - NM

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    return 关键字是必需的,同时提前移动嵌套函数。

    var body: some View {
      func checkDead() {
        if dead[currentNum] == 1 {
          return correct = true
        }
      }
    
      return VStack {
    

    【讨论】:

      【解决方案2】:

      只需将函数 checkDead 移出 body

      var body: some View {
          VStack {
              Text(person[currentNum])
              Button(action: {
                  
              }, label: {
                  Text("Dead")
              })
          }
      }
      
      func checkDead() {
          if dead[currentNum] == 1{
              return correct = true
          }
      }
      

      【讨论】:

        【解决方案3】:
        var body: some View {
                VStack{
                Text(person[currentNum])
                    
                    Button(action: {
                        
                    }, label: {
                        Text("Dead")
                    })
                    
                    
                    
                }
                
        // Problem Starts! :
                func checkDead() {
                    if dead[currentNum] == 1{
                        return correct = true
                    }
                }
        // Problem Ends!
                
            }
        

        您在变量内声明一个函数(在变量body 内声明函数checkDead

        长话短说,不要那样做!在类的范围内声明你的函数,而不是在正文中:

        struct ContentView: View {
            
            @State var currentNum = 0
            var person = ["Michael Jackson","Elton John","Prince"]
            var dead = [1,0,1]
            // 1 = yes 0 = no
            @State var correct = true
            
            var body: some View {
                VStack{
                Text(person[currentNum])
                    
                    Button(action: {
                        
                    }, label: {
                        Text("Dead")
                    })
                    
                    
                    
                }
                
                // func checkDead() {
                //    if dead[currentNum] == 1{
                //        return correct = true
                //    }
                //}
                // move it to the struct's scope and outside the variable `body`
            }
        
        //here is where it should be
         func checkDead() {
                    if dead[currentNum] == 1{
                        correct = true
                    }
                }
        
        }
        

        【讨论】:

        • 你还试图在你的函数中返回一些东西。我不知道你想做什么,但它认为这只是额外的,你应该删除它
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-28
        • 2021-11-02
        • 2015-02-24
        • 1970-01-01
        • 2020-09-28
        • 2013-06-20
        • 1970-01-01
        相关资源
        最近更新 更多