![](/img/trans.png)
[英]Contextual type NSIndexSet cannot be used with array literal for Swift 2.x
[英]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
而不是.cgColor
, CGGradientCreateWithColors
而不是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.