CNContact属性编码 [英] CNContact encoding of properties

查看:85
本文介绍了CNContact属性编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名字为André的v-card字符串,并用该v-card初始化了CNContact.

I have a v-card string with the first name André and I initialise a CNContact with the v-card.

BEGIN:VCARD

VERSION:2.1

N:Foo;André;;;

FN:André Foo

TEL;CELL:00023 4474848

END:VCARD

我用原始字符串初始化联系人,如下所示:

I initialise the contact with the raw string like this:

if let data = string.data(using: .utf8) {
    do {
        let contacts = try CNContactVCardSerialization.contacts(with: data)
        let contact = contacts.first
        return contact

    } catch {
        print("Data is not a VCard")
    }
}

但是当我打印出contact.givenName的原始字符串时,我得到:

But when I print out the raw string of contact.givenName I get:

André

如何在iOS中获取Contacts框架的正确字符串?

How can I get the proper string of the Contacts framework in iOS?

推荐答案

您需要在vcard字段中添加一个字符集,默认为ASCII.

You need to add a charset to the vcard fields, it defaults to ASCII.

BEGIN:VCARD

VERSION:2.1

N;CHARSET=UTF-8:Foo;André;;;

FN;CHARSET=UTF-8:André Foo

TEL;CELL:00023 4474848

END:VCARD

如果您想破解vcard中这种特定类型的错误,则可以手动将字符集注入其中:

If you want to hack around this specific type of error in the vcard then you can inject the charset manually into it:

let fixed = string.replacingOccurrences(of: "\nN:", with: "\nN;CHARSET=UTF-8:").replacingOccurrences(of: "\nFN:", with: "\nFN;CHARSET=UTF-8:")

这篇关于CNContact属性编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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