gpt4 book ai didi

arrays - 如何在 Swift 中访问 `AnyHashable` 中的 `Any` 类型?

转载 作者:行者123 更新时间:2023-11-28 06:32:45 24 4
gpt4 key购买 nike

我正在使用 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] 来访问单个联系人,尽管我如何才能访问更详细的信息,例如 compositeNamenumber?

我尝试了 serializedContacts[0].nameserializedContacts[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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com