在 Xcode 8 (Swift 3) 中将 Swift 数组转换为 CFArray [英] Converting Swift array to CFArray in Xcode 8 (Swift 3)

查看:25
本文介绍了在 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 就可以工作:

It works if you add the cast 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.

这篇关于在 Xcode 8 (Swift 3) 中将 Swift 数组转换为 CFArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆