2

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?

Shmidt
  • 16,436
  • 18
  • 88
  • 136

1 Answers1

3

Like this:

private let phoneLabels = [
    kABPersonPhoneMobileLabel,
    kABPersonPhoneIPhoneLabel,
    kABWorkLabel,
    kABHomeLabel,
    kABPersonPhoneMainLabel,
    kABPersonPhoneHomeFAXLabel,
    kABPersonPhoneWorkFAXLabel,
    kABPersonPhonePagerLabel,
    kABOtherLabel
] as [AnyObject] as! [String]
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • And you might want to file a bug report on this; I have. In my view, Swift should just "know" that a CFString is an NSString is a String. Nevertheless, this change _is_ consistent with the announced changes in Swift 1.2, so Apple may decide you don't have a case. You no longer get automatic implicit coercion of NSString to String, which is what your earlier code relied on. – matt Apr 09 '15 at 17:07
  • 1
    This is ridiculous. Not the answer, the syntax. Swift is (still) terrible at some places. – Léo Natan Apr 09 '15 at 17:09
  • One mark of a good language is allowing the developer to concentrate on the problem being solved, not the syntax the compiler requires. – zaph Apr 09 '15 at 17:28
  • @Zaph That train left the station in June 2014, when Swift decided to have the worst numerics handling in the history of computer languages (http://stackoverflow.com/questions/24108827/swift-numerics-and-cgfloat-cgpoint-cgrect-etc) Still, it's a lot better (and safer) than Objective-C, which _really_ required the developer to concentrate on the syntax (can you say "semicolon"?)... – matt Apr 09 '15 at 18:11
  • Yep, `[AnyObject] as! [String]` is so much beter than `;`. P.S. I had already up-voted your question. ;-) – zaph Apr 09 '15 at 19:18
  • Swift is a terrible language when you beyond the cookie cutter UI work. Anywhere where C/C++ interop is required, Swift being terrible is putting it mildly. – Léo Natan Apr 10 '15 at 18:21
  • or a bit more constrained `as [NSString] as! [String]` – vadian Sep 05 '15 at 09:48