user9269956
user9269956

Reputation: 85

What are possible reasons for the error: Value of type "x" has no subscripts?

This is my code. I have gone through it but can't seem to fix the error. What I need to do is get firebase information from the database and display it on my screen.

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()

    }

})

//Here is the Userx in a different file:

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
}

Once I'm done I want the firebase data to be fetched and displayed.

Upvotes: 0

Views: 215

Answers (2)

Haseeb Afsar
Haseeb Afsar

Reputation: 629

You've missed a curly brace in this file in the init

Your code

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
}

Fixed Code

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

Upvotes: 0

David Pasztor
David Pasztor

Reputation: 54716

The issue is that you override the instance property people with a local variable of another type, Userx instead of [Userx] in cellForRowAt. You should give another name to your local variable.

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
}

Unrelated to your problem, but you should also conform to the Swift naming convention, which is lowerCamelCase for variable names and use immutable non-Optional values where possible. Using a struct instead of a class also gives you an auto-synthetised memberwise initializer, so you don't need to create one yourself. You should also try to not use Any or AnyObject when you know the type a variable should hold (which should be the case in most situations).

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

Upvotes: 2

Related Questions