上下文类型不能与数组文字Swift 3颜色数组一起使用

时间:2017-02-10 04:58:28

标签: ios swift

我在尝试在Swift 3中创建数组时遇到问题。我的代码代码如下:

$(document).ready(function () {
        var todayDate = new Date();
        $('.datepicker').datepicker({
            format: 'mm-dd-yyyy',
            autoclose:true,    
            endDate: '-1d'
        }).on('changeDate', function (ev) {
            $(this).datepicker('hide');
        });


        $('.datepicker').keyup(function () {
            if (this.value.match(/[^0-9]/g)) {
                this.value = this.value.replace(/[^0-9^-]/g, '');
            }
        });
    });

我收到错误//// 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。但是我仍然看到这个错误,我在哪里错了?

1 个答案:

答案 0 :(得分:2)

如果您实际使用的是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),这是您的“实际”问题。