title: Trouble retrieving a CGColor from a Swift dictionary
body: <p>I need a Swift dictionary that can store any kind of object. Some of the values will be <code>CGColor</code> references. I have no issue creating the dictionary and storing the <code>CGColor</code> references. The problem is trying to safely get them back.</p>
  
  <pre><code>let color = CGColor(gray: 0.5, alpha: 1)
  var things = [String:Any]()
  things["color"] = color
  things["date"] = Date()
  print(things)
  </code></pre>
  
  <p>That works and I get reasonable output. Later on I wish to get the color (which may or may not exist in the dictionary. So naturally I try the following:</p>
  
  <pre><code>if let color = things["color"] as? CGColor {
      print(color)
  }
  </code></pre>
  
  <p>But this results in the error:</p>
  
  <blockquote>
    <p>error: conditional downcast to CoreFoundation type 'CGColor' will always succeed</p>
  </blockquote>
  
  <p>In the end I came up with:</p>
  
  <pre><code>if let val = things["color"] {
      if val is CGColor {
          let color = val as! CGColor
          print(color)
      }
  }
  </code></pre>
  
  <p>This works without any warnings in a playground but in my actual Xcode project I get a warning on the <code>if val is CGColor</code> line:</p>
  
  <blockquote>
    <p>'is' test always true because 'CGColor' is a Core Foundation type</p>
  </blockquote>
  
  <p>Is there good solution to this problem?</p>
  
  <p>I'm working with core graphics and layers and the code needs to work with both iOS and macOS so I'm trying to avoid <code>UIColor</code> and <code>NSColor</code>.</p>
  
  <p>I did find <a href="https://stackoverflow.com/questions/33589203/casting-from-anyobject-to-cgcolor-without-errors-or-warnings">Casting from AnyObject to CGColor? without errors or warnings</a> which is related but doesn't seem relevant any more since I don't need the parentheses to eliminate the warning plus I'm trying to use optional binding which isn't covered by that question.</p>