I declare in class ViewController an array map_sector_UIImage of UIImage.
But in viewDidLoad() I get error when appending 10 UIImage's to the array and error when I view.addSubview .
Each UIImage is to be a colored square
I get build errors (see below).
NOTE: let App.total_map_sectors: Int = 10
class ViewController: UIViewController
{
static var map_sector_UIImage: [UIImage] = []
override func viewDidLoad()
{
App.init_class()
// Allocate array of sector UIImage:
for map_sector_index in 0...App.total_map_sectors-1
{
ViewController.map_sector_UIImage[ map_sector_index ] +=
E R R O R Referencing operator function '+=' on 'FloatingPoint' requires that 'UIImage' conform to 'FloatingPoint'
{
let theImage = UIImage()
theImage.translatesAutoresizingMaskIntoConstraints = false //You need to call this property so the image is added to your view
return theImage
}()
}
super.viewDidLoad()
for map_sector_index in 0...App.total_map_sectors-1
{
view.addSubview( ViewController.map_sector_UIImage[ map_sector_index ] )
E R R O R Referencing operator function '+=' on 'FloatingPoint' requires that 'UIImage' conform to 'FloatingPoint'
}
for sector_index in 0...App.total_map_sectors - 1
{
ViewController.map_sector_UIImage[ sector_index ] =
// swift 4.0
UIColor.black.image(CGSize( width: App.sector_width_pix, height: App.sector_height_pix ))
// swift 3.0 = UIImage(color: .blackColor(), size: CGSize(width: App.sector_width_pix, height: App.sector_height_pix ))
// https://stackoverflow.com/questions/26542035/create-uiimage-with-solid-color-in-swift
// eg. let redImage200x200 = UIImage(color: .redColor(), size: CGSize(width: 200, height: 200))
}
}
// Swift 4.0
// From:
// https://stackoverflow.com/questions/26542035/create-uiimage-with-solid-color-in-swift
extension UIColor {
func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
return UIGraphicsImageRenderer(size: size).image { rendererContext in
self.setFill()
rendererContext.fill(CGRect(origin: .zero, size: size))
}
}
}