How to get localizedstring for CNLabeledValue in s

2019-02-17 02:35发布

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

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

How to get localizedstring for CNLabeledValue in swift3?

1条回答
Emotional °昔
2楼-- · 2019-02-17 02:47

In Swift 3, CNLabeledValue is declared as:

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

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.

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.

Try this:

 let localizedLabel = CNLabeledValue<NSString>.localizedString(forLabel: phoneNumber.label!)
查看更多
登录 后发表回答