簡體   English   中英

上下文類型不能與數組文字Swift 3顏色數組一起使用

[英]contextual type cannot be used with array literal Swift 3 colors array

嘗試在Swift 3中創建數組時,我的顏色有問題。我的代碼代碼如下:

//// Color Declarations
let gradientColor = UIColor(red: 0.859, green: 0.948, blue: 0.308, alpha: 1.000)
let gradientColor2 = UIColor(red: 0.979, green: 0.677, blue: 0.267, alpha: 1.000)
let colors = [gradientColor.CGColor, gradientColor.blendedColorWithFraction(0.5, ofColor: gradientColor2).CGColor, gradientColor2.CGColor] as CFArray
//// Gradient Declarations
let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), colors, [0, 0.5, 1])!

我得到的錯誤contextual type cannot be used with array literal 谷歌周圍尋求幫助,並說它將顏色數組投射到我已經完成的CFArray 但是我仍然看到這個錯誤,我在哪里錯了?

好奇如果你真的使用swift版本3,因為你使用2.x API( .CGColor而不是.cgColorCGGradientCreateWithColors而不是CGGradient的init),但無論如何在swift 3:

//: Playground - noun: a place where people can play

import UIKit

let gradientColor = UIColor(red: 0.859, green: 0.948, blue: 0.308, alpha: 1.000)
let gradientColor2 = UIColor(red: 0.979, green: 0.677, blue: 0.267, alpha: 1.000)

let colors = [gradientColor.cgColor, gradientColor2.cgColor]

let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors as CFArray, locations: [0, 0.5, 1])

我不熟悉blendedColorWithFraction因此必須是重命名的apple API或您正在使用的第三方。

編輯: blendedColorWithFraction用於NSColor (評論中提到@Tj3n),這是你的“實際”問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM