Ambiguous reference to member index '

I get this error: "Ambiguous reference to member index" when I try to change the color:

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) } 
+6
source share
1 answer

A float is not the same as a CGFloat. You must pass CGFloat to UIColor. Note. You must name your structures starting with a capital letter.

 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, blue: image[pixel].b, alpha: alpha ) } } 
+2
source

All Articles