错误的可能原因是什么:类型“ x”的值没有下标?

时间:2019-06-17 19:21:38

标签: swift

这是我的代码。我已经经历了,但似乎无法修复错误。我需要做的是从数据库中获取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数据。

2 个答案:

答案 0 :(得分:2)

问题在于,您用另一个类型的局部变量people而不是Userx中的[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
}

答案 1 :(得分:0)

您在init文件中错过了大括号

您的代码

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
}

固定代码

   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
    }
}