我正在使用 APAddressBook
从用户手机获取地址簿。当我打印变量 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";
})
]]
我可以通过简单地打印 serializedContacts[0]
来访问单个联系人,尽管我如何才能访问更详细的信息,例如 compositeName
和 number
?
我尝试了 serializedContacts[0].name
和 serializedContacts[0].phones
,但收到错误。
Value of type 'Any' has no member 'name'.
最佳答案
您还应该将之前尝试访问的属性转换为 AnyHashable。在您的情况下,这是:
serializedContacts[0][AnyHashable("name")]
关于arrays - 如何在 Swift 中访问 `AnyHashable` 中的 `Any` 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39864381/