2

I have stored a users home address in firebase as an array. It looks like this:

Image for User Home Address

I am now trying to grab all of my users data and store them into a dictionary like this:

Image for Storing in a Dictionary

The User() part of the code is defined above like this:

var users = [User]()

the [User]() part of it comes from a user.swift file which is here:

class User: NSObject {
    var fullName: String?
    var email: String!
    var userPhoto: String?
    var homeAddress: [String:[Double]]()
    var schoolOrWorkAddress: String!
}

the error is with the homeAddress part of it.

In the file I create variables for all my user data storage. My question now is how do I set home address to a value type of an array as that dictionaries key value. Can anyone help? Feel free to comment if you have any questions in helping me.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Rohan Vasishth
  • 169
  • 1
  • 13
  • 1
    http://stackoverflow.com/a/34466568/6297658 – Dravidian Aug 21 '16 at 10:35
  • PS: The home address acts as a key in the dictionary. I am struggling to set the value of that key which is an array of 2 coordinates. – Rohan Vasishth Aug 21 '16 at 15:19
  • Hi, I am new to swift and would love if you could show me how that links answer works? – Rohan Vasishth Aug 21 '16 at 15:25
  • Give your JSON tree , as text not an image, Also if you r new to Swift might i suggest you use `struct` instead of class and made an variable of type `struct_name` and called it to initialise your values which you'll have to retrieve separately – Dravidian Aug 21 '16 at 15:27
  • Never mind I figured it our. I simply had to do this: var homeAddress: NSArray – Rohan Vasishth Aug 21 '16 at 15:39

1 Answers1

0

You could try to override the setValue function to iterate through homeAddress points.

// value in this case would be the points in your dictionary and the
// the key would be "homeAddress"
override func setValue(value: AnyObject?, forKey key: String) {
    if key == "homeAddress" {

        let homeAddressPoints = [HomeAddressPoint]()
        for dict in value as! [[String: AnyObject]] {
            let point = HomeAddressPoint()
            point.setValuesForKeysWithDictionary(dict)
            homeAddressPoints?.append(point)
        }

    } else {
        super.setValue(value, forKey: key)
    }
}

This is just an idea, and hopefully you can build from this.

  • **Do not** override KVC methods unless you really need KVC which is not the case in this example. – vadian Aug 21 '16 at 15:00