How can I access `AnyHashable` types in `Any` in Swift?

女生的网名这么多〃 提交于 2019-12-20 04:08:39

问题


I'm using APAddressBook to get the address book from the users phone. it generates the following object of users when I print the variable serializedContacts:

[[
    AnyHashable("name"): {
        compositeName = "Mary Jones";
        firstName = Mary;
        lastName = Jones;
    },
    AnyHashable("recordID"): 111, 
    AnyHashable("phones"): <__NSSingleObjectArrayI 0x17401c160>({
           number = "0411 111 111";
    })
], 
[
    AnyHashable("name"): {
        compositeName = "Jack Smith";
        firstName = Jack;
        lastName = Smith;
    }, 
    AnyHashable("recordID"): 112, 
    AnyHashable("phones"): <__NSSingleObjectArrayI 0x17401c190>({
        number = "0422 222 222";
    })
]]

I can access a single contact by simply printing serializedContacts[0], although how can I access the finer details such as compositeName and number?

I tried serializedContacts[0].name and serializedContacts[0].phones, although receive the error.

Value of type 'Any' has no member 'name'.


回答1:


You should also convert the property you're trying to access to AnyHashable before. In your case this is:

serializedContacts[0][AnyHashable("name")]


来源:https://stackoverflow.com/questions/39864381/how-can-i-access-anyhashable-types-in-any-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!