如何索引到字典数组

时间:2015-11-15 12:52:27

标签: ios swift

我在控制台中有一个看起来像这样的对象。它看起来像一个里面有字典的数组。我怎样才能到达地址栏?

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;
    };
}>

我通过这种方法访问电子邮件密钥:

let emails = users.allDocuments[0].valueForKey("emails")

1 个答案:

答案 0 :(得分:1)

对象的内容是一个可选的数组。

有两种选择:

  • 安全访问:

    address
  • 如果确保数组中的字典和密钥let emails = users.allDocuments[0].objectForKey("emails")! let address = emails[0]["address"] as! String 存在:

    {{1}}