获取 CNLabeledValue 错误“'NSCopying & NSSecureCoding' 不能在 Xcode 10.2 中不在 Xcode 10.1 中

Getting CNLabeledValue error "'NSCopying & NSSecureCoding' cannot be in Xcode 10.2 not in Xcode 10.1

var currentContact = CNLabeledValue<NSCopying & NSSecureCoding>()

我想创建一个变量来存储来自联系人的值,该值可以是 phone 号码或电子邮件地址

var currentContact = CNLabeledValue<NSCopying & NSSecureCoding>()
currentContact = self.itemsInAcontact[section][0] as! CNLabeledValue


if ((currentContact.value as? CNPhoneNumber) != nil){
    phoneNumber = currentContact.value as! CNPhoneNumber

    if let y = phoneNumber?.value(forKey: "initialCountryCode"){
        cell.nameLabel!.text = "\(phoneNumber!.value(forKey: "initialCountryCode") as! String)\(phoneNumber!.stringValue)"
    }else{
        cell.nameLabel!.text = "\(phoneNumber!.stringValue)"
    }
}else{
    cell.nameLabel!.text = currentContact.value as! String
}

在这里,我试图在表格视图的单元格内显示无姓名类型联系人中可用的联系电话或电子邮件地址,但我在声明 var currenctContact

时遇到错误

错误消息:“'NSCopying & NSSecureCoding' 不能用作符合协议 'NSSecureCoding' 的类型,因为 'NSSecureCoding' 具有静态要求”。

LabeledValue 是泛型。两种不同的 LabeledValue 类型(即同一个泛型以两种不同的方式解析,CNLabeledValue<NSString>CNLabeledValue<CNPhoneNumber>)是不同的类型,不能存储在一个公共的 属性 中。这与 [Int][String] 是两种不同类型的事实没有什么不同,尽管它们都是数组。

您可以在单个 属性 中存储两种不同的 LabeledValue 类型的唯一方法是将 属性 键入为 AnyObject。因此,这有效:

var currentContact : AnyObject? = nil

let phoneNumber = CNPhoneNumber(stringValue: "1234567890")
let labelled = CNLabeledValue(label: "yoho", value: phoneNumber)
currentContact = labelled

let email = CNLabeledValue(label: "hoha", value: "mickey@mouse.com" as NSString)
currentContact = email

但是,我不建议这样做。相反,因为你真正需要的只是一个字符串,所以让你的 currentContact 成为一个包含 NSString 的标签值:

var currentContact : CNLabeledValue<NSString>? = nil

您可以将电子邮件 CNLabeledValue 直接存储到其中。对于 phone 数字,从 phone 数字的字符串值形成一个新的标记值:

currentContact = CNLabeledValue(
    label:phone.label, value:phone.value.stringValue as NSString)

看起来 Apple 在 XCode 10.2 中更改了 NSSecureCoding 上的某些内容,但我还找不到任何详细信息。

所以,现在,您应该从 NSCopying & NSSecureCoding 更改为 NSString

var currentContact = CNLabeledValue<NSString>()