iosswiftcfstring

'[CFString!]' is not convertible to '[String]'


Before Swift 1.2 I had following array:

private let phoneLabels = [
    kABPersonPhoneMobileLabel,
    kABPersonPhoneIPhoneLabel,
    kABWorkLabel,
    kABHomeLabel,
    kABPersonPhoneMainLabel,
    kABPersonPhoneHomeFAXLabel,
    kABPersonPhoneWorkFAXLabel,
    kABPersonPhonePagerLabel,
    kABOtherLabel
] as [String]

After I updated Xcode to 6.3, I can't have it like that:

private let phoneLabels = [
    kABPersonPhoneMobileLabel,
    kABPersonPhoneIPhoneLabel,
    kABWorkLabel,
    kABHomeLabel,
    kABPersonPhoneMainLabel,
    kABPersonPhoneHomeFAXLabel,
    kABPersonPhoneWorkFAXLabel,
    kABPersonPhonePagerLabel,
    kABOtherLabel
] as! [String]

Because compiler shows me an error: '[CFString!]' is not convertible to '[String]'.

I can probably convert each CFString to String in array, but maybe there is an easier and more readable way to fix that?


Solution

  • Like this:

    private let phoneLabels = [
        kABPersonPhoneMobileLabel,
        kABPersonPhoneIPhoneLabel,
        kABWorkLabel,
        kABHomeLabel,
        kABPersonPhoneMainLabel,
        kABPersonPhoneHomeFAXLabel,
        kABPersonPhoneWorkFAXLabel,
        kABPersonPhonePagerLabel,
        kABOtherLabel
    ] as [AnyObject] as! [String]