如何在Swift中访问"Any"中的"AnyHashable"类型? [英] How can I access `AnyHashable` types in `Any` in Swift?

查看:633
本文介绍了如何在Swift中访问"Any"中的"AnyHashable"类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 APAddressBook 从用户电话中获取通讯录.当我打印变量serializedContacts时,它会生成以下用户对象:

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";
    })
]]

我可以通过简单地打印serializedContacts[0]来访问单个联系人,尽管我如何访问compositeNamenumber之类的更详细的信息?

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

尽管收到错误,我还是尝试了serializedContacts[0].nameserializedContacts[0].phones.

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

任何"类型的值没有成员名称".

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

推荐答案

您还应该将之前尝试访问的属性转换为AnyHashable.您的情况是:

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

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

这篇关于如何在Swift中访问"Any"中的"AnyHashable"类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆