如何在swift3中获取CNLabeledValue的localizedstring

时间:2016-07-11 05:11:26

标签: ios swift swift3 cncontact

在swift 2中我使用CNLabeledValue.localizedStringForLabel(phoneNumber.label)并且工作正常。

在swift 3中,我尝试了这一行CNLabeledValue.localizedString(forLabel: phoneNumber.label!)但得到了generic parameter 'ValueType' could not be inferred错误

如何在swift3中获取CNLabeledValue的localizedstring?

1 个答案:

答案 0 :(得分:29)

在Swift 3中,CNLabeledValue被声明为:

public class CNLabeledValue<ValueType : NSCopying, NSSecureCoding> : NSObject, NSCopying, NSSecureCoding {
    //...
}

它是一种泛型类型,如果您在适当的上下文中使用它,则无需强制转换value。 Swift 3很好地推断了ValueType

但在你的代码中,Swift无法推断出ValueType。这有点烦人,因为ValueType在执行类型方法时是不必要的。但是Swift的类型系统需要指定它。如果Swift无法推断ValueType的类型,您可以明确地给它。

试试这个:

 let localizedLabel = CNLabeledValue<NSString>.localizedString(forLabel: phoneNumber.label!)