在Xcode 8(Swift 3)中将Swift数组转换为CFArray

在Xcode 8(Swift 3)中将Swift数组转换为CFArray

问题描述:

这不再适用于Xcode 8 beta 6:

This no longer works in Xcode 8 beta 6:

let colors:CFArray = [fromColor.cgColor, toColor.cgColor]

let gradient:CGGradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors:[fromColor.cgColor, toColor.cgColor], locations:[0.0, 1.0])!

错误是:上下文类型'CFArray'不能与数组文字一起使用

The error is: Contextual type 'CFArray' cannot be used with array literal

从数组转换为CFArray的最新方法是什么?

What's the latest way to convert from array to CFArray?

如果添加强制转换as CFArray:

let colors = [fromColor.cgColor, toColor.cgColor] as CFArray

或者您可以在通话中添加演员表

or you can add the cast in a call:

let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors:[fromColor.cgColor, toColor.cgColor] as CFArray, locations:[0.0, 1.0])!

在Swift 3(Xcode 8 beta 6)中,隐式转换为桥接类型

In Swift 3 (Xcode 8 beta 6), implicit casting to bridged types has been removed. In some cases, like this one, you will need to add explicit casting to make it work.