2016-08-21 33 views
1

我已经将用户的家庭地址作为数组存储在firebase中。它看起来像这样:如何在字典键值中将变量的值设置为数组?

Image for User Home Address

我现在想要抓住我的所有用户数据,并将它们存储到一个像这样的词典:

Image for Storing in a Dictionary

代码的User()部分定义如上:

var users = [User]() 

[User]()它的一部分来自用户.swift文件,该文件是在这里:

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

错误是它的homeAddress部分。

在文件中,我为我的所有用户数据存储创建了变量。我现在的问题是如何将家庭地址设置为数组的值类型作为该字典的键值。谁能帮忙?如果您有任何问题帮助我,请随时发表评论。

+1

http://stackoverflow.com/a/34466568/6297658 – Dravidian

+0

PS:家庭地址充当字典中的键。我正在努力设置该键是2个坐标数组的值。 –

+0

嗨,我是新来的迅速,并会爱,如果你能告诉我如何链接回答作品? –

回答

0

您可以尝试覆盖setValue函数以迭代homeAddress点。

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

这只是一个想法,并希望你可以从此建立。

+0

**不要**覆盖KVC方法,除非你真的需要KVC,在这个例子中不是这种情况。 – vadian

相关问题