Unexpected nil when using Chameleon's result to set UIView.backgroundColor

173 Views Asked by At

I'm trying to use Chameleon to get analogous colors. The returned array of colors is not nil, each of the 5 elements are also not nil. However the code "tintView.backgroundColor = color" results in "Fatal error: Unexpectedly found nil while unwrapping an Optional value". The first picture shows the code and the non-nil 5 element array returned from a call to Chameleon. The 2nd picture shows the variable as copied from the global array (it's missing data)

1

I've tried setting Chameleon to use Swift 4 as well as Swift 4.2. No difference. I'm currently targeting iPhone 7 running IOS 11.4 What am I missing? Shouldn't this simply work?

Pertinent part of Globals.swift:

  static var companionColors = ColorSchemeOf(.analogous, color: UIColor.flatYellowDark, isFlatScheme: true)
  static var complimentryColor = UIColor(complementaryFlatColorOf:selectedColor)
  static var initialColorName = "Yellow"
  static var selectedColor = UIColor.flatYellowDark {
      didSet {
          companionColors = ColorSchemeOf(.analogous, color: selectedColor, isFlatScheme: true)
          complimentryColor = UIColor(complementaryFlatColorOf:selectedColor)
        }
    }

CommonViewController:

  class CommonViewController: UIViewController {
    @IBOutlet weak var canvas: UIImageView!
    @IBOutlet weak var tintView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
      let color = Globals.companionColors[1]
      tintView.backgroundColor = color
    }
  }

As mentioned the line 'tintView.backgroundColor = color' is throwing the error. Globals.companionColors contains 5 elements of type UIDeviceRGBColor* with RGBA values and a UIColor component. 'color' is an NSObject.

1

There are 1 best solutions below

3
Alexander C On

Try removing everything in viewDidAppear and replacing with:

var colorArray = NSArray(ofColorsWithColorScheme:ColorScheme.Analogous, with:UIColor.flatRedColor(), flatScheme:true)

tintView.backgroundColor = colorArray[1]

Make sure Chameleon is installed and imported correctly. Of course you’d modify flatRedColor to the color of your choice but perhaps try this code first and see if it works. My guess is there is a problem in Globals but without seeing the code for it I cannot know for sure.