0

Looking for assistance since I am missing something! I have defined a new object called "User".

class User: NSObject {

    var userID: String!
    var fullname: String!
    var imagePath: String!
}

Now I want to iterate through each object one at a time checking the values of some of the fields.

func retrieveUsers() {
        let ref = Database.database().reference()
        ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot: DataSnapshot) in
            let users = snapshot.value as! [String: AnyObject]
            self.user.removeAll()
            
            for (key,value) in users {
    // HOW DO I REFERENCE A FIELD IN THE USER OBJECT
            }
          }

1 Answers1

0

Keep in mind that in your code, your users object is of type [String:AnyObject] since you're grabbing it from a database (Firebase?).

So, you have an untyped Dictionary to deal with.

You can reference fields on the dictionary by doing users[key] where key is a String.

Judging by your naming of variables, it looks like you're expecting users to be an array rather than a dictionary, but then you're casting it as a dictionary. Without knowing your database schema it's hard to say what's actually happening.

But, you most likely want to actually turn your [String:AnyObject] into your actual data type. There are a number of approaches to this, but you may have to write your own decoder.

You may want to add more information about what database you're actually using.

Update: Including an example method for turning your dictionary into your object:

class User: NSObject {
    var userID: String
    var fullname: String
    var imagePath: String

    required init(withDictionary dict : [String: AnyObject]) {
        userID = (dict["userID"] as? String) ?? ""
        fullname = (dict["fullname"] as? String) ?? ""
        imagePath = (dict["imagePath"] as? String) ?? ""
    }
}

Note that I'm not handling failures -- just putting in empty strings.

You can also look into storing making your model Codable compliant and try to convert in and out of JSON. See: How can I use Swift’s Codable to encode into a dictionary?

jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • Yes I am wrestling with Firebase (realtime db). So what is the easiest way to deal with this object? Is it easiest to map users into a Dictionary? I tried to cast users to STRING: Users but that was a failed attempt - "Could not cast value of type '__NSDictionaryM'" – Bruce Simmons Jan 10 '21 at 23:17
  • for (key,value) in users { print (users) } does print out all users and their fields – Bruce Simmons Jan 10 '21 at 23:23
  • You can't cast directly from AnyObject because Firebase doesn't know anything about how to convert. You will probably have to write an initializer method that takes a [String: AnyObject] and converts it to your fields. Will edit my answer in a second... – jnpdx Jan 10 '21 at 23:25
  • Thanks. Or is it eaiser to forget the OBJECT and just define a dictionary? – Bruce Simmons Jan 10 '21 at 23:38
  • That’s your choice depending on how you use it. If my answer helped please feel free to upvote/accept – jnpdx Jan 10 '21 at 23:41