0

I have an app in Android and I'm building the iOS version of it, but when I try to fetch user data into my model class I get an error, i know its because some nodes dont have all keys inside it, but this is ok on Android, i dont know how to deal with it on Swift 3. You can see my data on the link in the end of this post.

I get this doing a .childadded:

Fatal error: unexpectedly found nil while unwrapping an Optional value

This is my model class:

struct User {
var name: String
var age: String
var gender: String
var description: String
var hair:String
var skin:String
var tel:String

init(name: String, age: String, gender: String, hair: String, skin: String, tel: String, description: String) {
    self.name = name
    self.age = age
    self.gender = gender
    self.description = description
    self.hair = hair
    self.skin = skin
    self.tel = tel
}

init(snapShot: FIRDataSnapshot){
    self.name = (snapShot.value! as! NSDictionary)["name"] as! String
    self.age = (snapShot.value! as! NSDictionary)["age"] as! String
    self.gender = (snapShot.value! as! NSDictionary)["gender"] as! String
    self.description = (snapShot.value! as! NSDictionary)["description"] as! String
    self.hair = (snapShot.value! as! NSDictionary)["hair"] as! String
    self.skin = (snapShot.value! as! NSDictionary)["skin"] as! String
    self.tel = (snapShot.value! as! NSDictionary)["tel"] as! String

}

func toAnyObject() -> [String: Any]{
    return ["name": name, "age": age, "gender": gender, "description": description, "hair": hair, "skin": skin, "tel": tel]
}  
}

This is my fetch code:

ref.child("users").observe(.childAdded, with: { (snapshot) in
      if(snapshot.value is NSNull){

        }else{
            let user = User(snapShot: snapshot)
            print("snapshot not nil: \(snapshot.key) \(snapshot.value)")
            print("user: \(user.name)")
        }

It will have some data empty, like skin and description, see? I have some register with less keys, in android this is ok, but on swift it cant handle nil

The first register works ok, but the second crash My data base

fatal error: unexpectedly found nil while unwrapping an Optional value 2016-12-21 14:13:46.765206 GPMap[6033:1579494] fatal error: unexpectedly found nil while unwrapping an Optional value

1
  • You've included a link to a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in your Firebase Database console. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. Commented Dec 26, 2016 at 0:05

1 Answer 1

1

Change your function to look like this to check if the values are present:

init(snapShot: FIRDataSnapshot){

     self.name = (snapshot.value as? NSDictionary)?["name"] as? String ?? ""

     // ... continue for other values

}

Then, you do not need to check if the values are Null (therefore no error):

ref.child("users").observe(.childAdded, with: { (snapshot) in

        let user = User(snapShot: snapshot)

        // use the method from User to parse the data
        let userData = user.toAnyObject()
        print(userData)    //Dictionary of data variables

        print("snapshot not nil: \(snapshot.key) \(snapshot.value)")
        print("user: \(user.name)")

    })
2
  • the intent to use let user = User(snapShot: snapshot) is to parse all information in one linem i will do this test. Commented Dec 25, 2016 at 22:58
  • Tks @Nathan-levitt,this works, but its not yet what i want, that is user this model class User to parse all the data. Commented Dec 25, 2016 at 23:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.