7

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?

Aldo Lazuardi
  • 1,898
  • 2
  • 19
  • 34

1 Answers1

33

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!)
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • Can you provide any additional explanation on why it is necessary to specify `NSString` rather than `String`? – Murray Sagal Sep 19 '16 at 13:57
  • 3
    @MurraySagal, `CNLabeledValue`s generic parameter is declared as ``. So, in this case, you can choose any type which conforms to `NSCopying` and `NSSecureCoding`. `NSString` does and `String` does not. – OOPer Sep 19 '16 at 20:02
  • 2
    Thank you for figuring this out. Why it's not part of the Swift 3 Converter in XCode escapes me... – Joshua Pinter Oct 09 '16 at 18:57