如何在swift3中获取CNLabeledValue的localizedstring [英] How to get localizedstring for CNLabeledValue in swift3

查看:447
本文介绍了如何在swift3中获取CNLabeledValue的localizedstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In swift 2 I'm using CNLabeledValue.localizedStringForLabel(phoneNumber.label) and works fine.

在swift 3中,我试过这行 CNLabeledValue.localizedString(forLabel:phoneNumber.label!)但得到无法推断出通用参数'ValueType'错误

In swift 3 I tried this line CNLabeledValue.localizedString(forLabel: phoneNumber.label!) but got generic parameter 'ValueType' could not be inferred error

如何在swift3中获取CNLabeledValue的localizedstring?

How to get localizedstring for CNLabeledValue in swift3?

推荐答案

在Swift 3中, CNLabeledValue 被声明为:

In Swift 3, CNLabeledValue is declared as:

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

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

It's a generic type and if you use it in a proper context, you have no need to to cast its value. Swift 3 well infers the ValueType.

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

But in your code, Swift has no clue to infer the ValueType. It is sort of annoying, because ValueType is needless while executing the type method. But the type system of Swift needs it to be specified. If Swift cannot infer the type of the ValueType, you can explicitly give it.

试试这个:

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

这篇关于如何在swift3中获取CNLabeledValue的localizedstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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