How can I access `AnyHashable` types in `Any` in S

2020-04-29 16:25发布

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条回答
ゆ 、 Hurt°
2楼-- · 2020-04-29 16:51

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

serializedContacts[0][AnyHashable("name")]
查看更多
登录 后发表回答