Swift UIColor初始化程序 - 仅在定位iPhone5时出现编译器错误 [英] Swift UIColor initializer - compiler error only when targeting iPhone5s

查看:135
本文介绍了Swift UIColor初始化程序 - 仅在定位iPhone5时出现编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新项目中工作: iOS>应用程序>游戏> SpriteKit,Swift,iPhone

Working from a new project: iOS > Application > Game > SpriteKit, Swift, iPhone

这里我在 GameScene.swift 中编写了一个函数来接受十六进制颜色值和alpha double。我将使用Swift文档,并引用我在网上看到的优雅解决方案。我注意到这个确切的代码在模拟器中编译,运行并显示 iPhone5 的构建目标的正确颜色。

Here I've written a function in my GameScene.swift to accept a hex color value and alpha double. I am going by the Swift documentation, and referencing an elegant solution I saw online. I've noticed this exact code compiles, runs and displays the proper color for a build target of iPhone5 in the simulator.

当我的构建目标设置为 iPhone5s 时,完全相同的项目甚至无法编译。

The exact same project wont even compile when my build target is set for iPhone5s.


无法找到接受所提供参数的'init'的重载。

Could not find an overload for 'init' that accepts the supplied arguments.

函数:

// colorize function takes HEX and Alpha converts then returns aUIColor object
func colorize (hex: Int, alpha: Double = 1.0) -> UIColor {
    let red = Double((hex & 0xFF0000) >> 16) / 255.0
    let green = Double((hex & 0xFF00) >> 8) / 255.0
    let blue = Double((hex & 0xFF)) / 255.0
    var color: UIColor = UIColor( red: Float(red), green: Float(green), blue: Float(blue), alpha:Float(alpha) )
    return color
}

didMoveToView

override func didMoveToView(view: SKView) {

    // blue background color
    self.backgroundColor = colorize( 0x003342, alpha:1.0)

}

不可否认我是iOS开发的新手,但我想知道是什么导致编译器找不到这个初始值设定项?

Admittedly I am very new to iOS development, but I am wondering what is going on to cause the compiler not to find this initializer?

编辑:暂时建立一个github

快速提供相关文件......我唯一的一个我编辑的是GameScene.swift

推荐答案

这可能与Float和Double之间的不匹配有关。基本上,如果参数预期为CGFloats,则需要传递CGFloats。

It probably has to do with a mismatch between Float and Double. Basically, if the arguments are expected to be CGFloats, you need to pass CGFloats.

let color = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))

这里需要了解的重要事项是CGFloat在32位平台上定义为Float,在64位平台上定义为Double。因此,如果您明确使用Floats,Swift在32位平台上不会出现问题,但会在64位上产生错误。

What's important to understand here is that CGFloat is defined as Float on 32 bit platforms, and Double on 64 bit platforms. So, if you're explicitly using Floats, Swift won't have a problem with this on 32 Bit platforms, but will produce an error on 64 bit.

这篇关于Swift UIColor初始化程序 - 仅在定位iPhone5时出现编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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