' [!CFString字符串]'不能转换为' [String]'

时间:2015-04-09 16:55:15

标签: ios swift cfstring

在Swift 1.2之前,我有以下数组:

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

在我将Xcode更新为6.3后,我不能这样:

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

因为编译器向我显示错误:'[CFString!]' is not convertible to '[String]'

我可以将数组中的每个CFString转换为String,但也许有一种更简单,更易读的方法来修复它?

1 个答案:

答案 0 :(得分:3)

像这样:

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