我收到此错误:“当我尝试更改颜色时,对成员’下标’的模糊引用”:
struct color { var r : Float var g : Float var b : Float } func setPixels(image:[color],pixel: Int) { let alpha: Float = 1.0 let pixelView = view.viewWithTag(pixel) as! UIImageView pixelView.backgroundColor = UIColor( red: image[pixel].r,//Error: Ambiguous reference to member 'subscript' green: image[pixel].g,blue: image[pixel].b,alpha: alpha) }
解决方法
浮动它与CGFloat不同.你必须将CGFloat传递给UIColor.注意:您应该以大写字母开头命名结构.
struct Color { let r: CGFloat let g: CGFloat let b: CGFloat } class ViewController: UIViewController{ func setPixels(image: [Color],pixel: Int) { let alpha: CGFloat = 1 let pixelView = view.viewWithTag(pixel) as! UIImageView pixelView.backgroundColor = UIColor( red: image[pixel].r,green: image[pixel].g,alpha: alpha ) } }