swift type of expression is ambiguous without more context

这个提示可能是输入参数类型和实际函数参数类型不一致导致的。

错误代码:

convenience init(hex: Int, alpha: CGFloat = 1.0) {
            let components = (
                R: Double((hex >> 16) & 0xff) / 255.0,
                G: Double((hex >> 08) & 0xff) / 255.0,
                B: Double((hex >> 00) & 0xff) / 255.0
                    )
                
            self.init(
                red: components.R,
                green: components.G,
                blue: components.B,
                alpha: alpha
            )
        }

正确代码:

convenience init(hex: Int, alpha: CGFloat = 1.0) {
            let components = (
                R: CGFloat((hex >> 16) & 0xff) / 255.0,
                G: CGFloat((hex >> 08) & 0xff) / 255.0,
                B: CGFloat((hex >> 00) & 0xff) / 255.0
                    )
                
            self.init(
                red: components.R,
                green: components.G,
                blue: components.B,
                alpha: alpha
            )
        }

函数实际定义参数是CGFloat:

public init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)