我有一个在控制台中看起来像这样的对象。它看起来像一个数组,里面有一个字典。我怎样才能到达地址字段?
Optional((
{
address = "xxxxxxx@gmail.com";
verified = 1;
}
))
该对象来自此对象:
<METDocument key: <collection: users, ID: KTsCySacEAiz3eDnf>, fields: {
emails = (
{
address = "xxxxxxx@gmail.com";
verified = 1;
}
);
profile = {
address = {
city = Oslo;
country = nor;
lineOne = "address";
lineTwo = "";
zip = 0264;
};
birthdate = "1985-09-01 23:00:00 +0000";
card = {
last4 = 4242;
verified = 1;
};
filledOut = 1;
gender = Male;
name = {
first = "Elon";
last = "Musk";
};
phone = 911;
validated = 1;
};
}>
我通过以下方法访问电子邮件 key :
let emails = users.allDocuments[0].valueForKey("emails")
最佳答案
对象的内容是一个可选的数组。
有两个选项:
安全访问:
if let emails = users.allDocuments[0].objectForKey("emails") where emails.count > 0 { if let address = emails[0]["address"] as? String { print(address) } }
如果保证数组中的字典和键
address
存在:let emails = users.allDocuments[0].objectForKey("emails")! let address = emails[0]["address"] as! String
关于ios - 如何索引字典数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33719881/