问题
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