Setting my UIView .backgroundColor from an item in my CGColor array causes build error:
"Type of expression is ambiguous without more context"
(at bottom of code)
I want to display UIView rectangles and control the color of each.
Here's the code -- build error at bottom..
class ViewController: UIViewController
{
// the array of UIView's, one for each rectangle:
static var map_sectors_UIView: [UIView] = []
override func viewDidLoad()
{
// init the UIView array:
for map_sector_index in 0...App.total_map_sectors-1
{
let new_UIView = UIView()
new_UIView.translatesAutoresizingMaskIntoConstraints = false //You need to call this property so the image is added to your view
ViewController.map_sectors_UIView[ map_sector_index ] = new_UIView
}
super.viewDidLoad()
// Add the subviews:
for map_sector_index in 0...App.total_map_sectors-1
{
view.addSubview( ViewController.map_sectors_UIView[ map_sector_index ] )
}
. . .
// init each UIView rectangle:
ViewController.map_sectors_UIView[ map_sector_index ].frame = CGRect( x: map_sector_column * App.sector_width_pix,
y: map_sector_row * App.sector_height_pix,
width: App.sector_width_pix,
height: App.sector_height_pix )
ViewController.map_sectors_UIView[ map_sector_index ].backgroundColor = .green
. . .
class App
{
static var sector_colors_array: [CGColor] = []
. . .
static func init_class()
{
// Declare array of colors for the UIView rectangles:
sector_colors_array = [CGColor]( repeating: UIColor.black.cgColor,
count: total_map_sectors )
}
. . .
// Display all the rectangles:
DispatchQueue.main.sync
{
ViewController.map_sectors_UIView[ sector_index ].backgroundColor = sector_colors_array[ sector_index ]
.......... gets build error: "Type of expression is ambiguous without more context"
Thanks, Matt. Easy to fix. Just replaced references to CGColor with UIColor.