Skip to content

Instantly share code, notes, and snippets.

@stevecass
Last active April 17, 2016 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevecass/0f4b79c088f128cbe7a1ec939d5739e2 to your computer and use it in GitHub Desktop.
Save stevecass/0f4b79c088f128cbe7a1ec939d5739e2 to your computer and use it in GitHub Desktop.
import UIKit
let nonOptionalVar: [String] = ["a", "b", "c"]
let optionalVar:String? = "jimmy"
// this compiles OK with import UIKit present
let ex1: [String: AnyObject] = ["key" : nonOptionalVar, "notherkey": ["content": optionalVar!]]
// Here we forget the ! character that unwraps the optional
// error: contextual type 'AnyObject' cannot be used with dictionary literal
let ex2: [String: AnyObject] = ["key" : nonOptionalVar, "notherkey": ["content": optionalVar]]
/*
If we omit import UIKit both both cases error with a different message
error: value of type '[String]' does not conform to expected dictionary value type 'AnyObject'
[Sratches head]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment