【问题标题】:Value of type 'Any' has no member 'title' [closed]“任何”类型的值没有成员“标题”[关闭]
【发布时间】:2021-08-13 15:29:30
【问题描述】:

我正在尝试在列表中显示来自数组的数据列表,但无法正常工作,我在下面收到此错误

还有其他方法可以将数组中的数据显示到列表中吗?

谢谢:-)

错误: Value of type 'Any' has no member 'title'

我的代码:

import SwiftUI
import FirebaseAuth
import FirebaseDatabase
import Foundation

struct goal: Identifiable {
    var id: String
    var description: String
    var endDate: String
    var category: String
}


struct ActiveGoalsView: View {
    
    @State var goals = []
    @State var ref = Database.database().reference()
    @State private var multiSelection = Set<UUID>()

    func getData() {
        ref.child("users").child(Auth.auth().currentUser?.uid ?? "noid").child("goals").observeSingleEvent(of: .value) { snapshot in
            
            for snap in snapshot.children {
                
                let snap1 = snap as! DataSnapshot
                
                let goalId = snap1.childSnapshot(forPath: "goalId").value
                let description = snap1.childSnapshot(forPath: "description").value
                let endDate = snap1.childSnapshot(forPath: "end_date").value
                let category = snap1.childSnapshot(forPath: "category").value
                
                goals.append(goal(id: goalId as! String, description: description as! String, endDate: endDate as! String, category: category as! String))
                
                //print(snap1.childSnapshot(forPath: "goalId").value)
                //print(snap)
                
                print(goals)
            }
        }
    }
    
    
    
    var body: some View {
        NavigationView {
            
            ForEach(goals, id: \.self) {goal in
                Text(goal.title)
            }
            
            List() {
                
                Button(action: {getData()}, label: {
                    Text("Button")
                })
                
                /*
                ForEach(goals, id: \.self) {goals in
                    HStack {
                        Button(action: {}, label: {
                            Text(goals)
                        })
                    }
                }*/
            }.navigationBarHidden(true)
        }
    }
}

struct ActiveGoalsView_Previews: PreviewProvider {
    static var previews: some View {
        ActiveGoalsView()
    }
}

【问题讨论】:

    标签: ios swift firebase swiftui


    【解决方案1】:

    您没有定义goals 应该包含什么类型——您只是使用了[],这意味着Any

    @State var goals = []
    

    所以,将其替换为:

    @State var goals: [goal] = []
    

    既然 Swift 知道 goalsgoal 的数组(符合 Identifiable),您可以删除 ForEach 中的 , id: \.self。此外,Text(goal.title) 没有意义,因为goal 没有名为title 的属性。也许你的意思是description

    注意:您应该将 goal 之类的结构大写 -> Goal

    注 2:Text(goal) 也没有意义。你的意思可能是Text(goal.description)

    【讨论】:

    • 不工作。当我尝试仅打印目标数组时,我遇到了其他错误,例如:Generic struct 'ForEach' requires that 'goal' conform to 'Hashable'Initializer 'init(_:)' requires that 'goal' conform to 'StringProtocol'
    • 还有其他方法可以显示数组中的数据吗??
    • @GSepetadelis 尝试用ForEach(goals) 替换ForEach(goals, id: \.self)
    • 现在我在 Text() 行收到此错误:Initializer 'init(_:)' requires that 'goal' conform to 'StringProtocol'
    • @GSepetadelis 使用Text(goal.description) 而不是Text(goal)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2017-09-28
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多