gpt4 book ai didi

swift - 错误 : Value of type "x" has no subscripts? 的可能原因是什么

转载 作者:行者123 更新时间:2023-11-28 11:31:23 26 4
gpt4 key购买 nike

这是我的代码。我已经完成了,但似乎无法修复错误。我需要做的是从数据库中获取 firebase 信息并将其显示在我的屏幕上。

class homepage:UITableViewController, CLLocationManagerDelegate{
var people = [Userx]()

public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
as! ViewControllerTableViewCell

let people: Userx


people = people[indexPath.row]
cell.lblName.text = people.Education
cell.lblgenre.text = people.WhatIamConsideringBuying

return cell


}



@IBOutlet weak var table: UITableView!
var locationManager = CLLocationManager()

override func viewDidLoad() {

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(signOut))

super.viewDidLoad()

let databaseRef = Database.database().reference()
databaseRef.child("Education").observe(DataEventType.value, with: {snapshot in

if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
let peopleObject = people.value as? [String: AnyObject]
let peopleEducation = peopleObject?["Education"]
let peopleWhatIamConsideringBuying = peopleObject?["WhatIamConsideringBuying"]
let peoplePhotoPosts = peopleObject?["PhotoPosts"]
let people = Userx(Education: peopleEducation as! String?, WhatIamConsideringBuying: peopleWhatIamConsideringBuying as! String?, PhotoPosts: peoplePhotoPosts as AnyObject)
self.people.append(people)

}
self.table.reloadData()

}

})

//这是另一个文件中的Userx:

class Userx {
var Education: String?
var WhatIamConsideringBuying: String?
var PhotoPosts: AnyObject?

init(Education: String?, WhatIamConsideringBuying: String?, PhotoPosts: AnyObject? ){

self.Education = Education
self.WhatIamConsideringBuying = WhatIamConsideringBuying
self.PhotoPosts = PhotoPosts
}

完成后,我希望获取并显示 Firebase 数据。

最佳答案

问题是您用另一种类型的局部变量 Userx 覆盖了实例属性 people 而不是 [Userx] cellForRowAt。您应该为局部变量指定另一个名称。

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
as! ViewControllerTableViewCell

let person: Userx = people[indexPath.row]

cell.lblName.text = person.Education
cell.lblgenre.text = person.WhatIamConsideringBuying

return cell
}

与您的问题无关,但您还应该遵守 Swift 命名约定,即变量名采用 lowerCamelCase,并尽可能使用不可变的非可选值。使用 struct 而不是 class 还可以为您提供一个自动合成的成员初始化器,因此您无需自己创建一个。当您知道变量应该持有的类型时,您还应该尽量不要使用 AnyAnyObject(在大多数情况下应该是这种情况)。

class Userx {
let education: String
let whatIamConsideringBuying: String
let photoPosts: AnyObject
}

关于swift - 错误 : Value of type "x" has no subscripts? 的可能原因是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56637636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com