-1

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.

Carl
  • 43
  • 6
  • This is not a duplicate. I understand the nil when unwrapping an optional. In this case none of the objects in question are nil. So why am I getting the error? – Carl Jun 05 '19 at 13:46

1 Answers1

0

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.

Alexander C
  • 676
  • 1
  • 5
  • 12
  • This code resulted in the same runtime error. I've added code to the original post. – Carl Jun 05 '19 at 14:28
  • changing 'tintView.backgroundColor = UIColor.flatYellowDark' results in the same error. – Carl Jun 05 '19 at 14:37
  • Im at a loss right now. I’d need to get to a computer to try running the code but it seems bizarre that setting the background color to flatYellowDark would result in the same error. Is Xcode saying the error is appearing on that line? If so I’d try cleaning and rebuilding the project. – Alexander C Jun 05 '19 at 17:42