Skip to content

Instantly share code, notes, and snippets.

@stevecass
Last active April 17, 2016 18:28
Show Gist options
  • Select an option

  • Save stevecass/0f4b79c088f128cbe7a1ec939d5739e2 to your computer and use it in GitHub Desktop.

Select an option

Save stevecass/0f4b79c088f128cbe7a1ec939d5739e2 to your computer and use it in GitHub Desktop.

Revisions

  1. Steven Cassidy revised this gist Apr 17, 2016. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions optional-literal-gotcha.swift
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,9 @@ let ex1: [String: AnyObject] = ["key" : nonOptionalVar, "notherkey": ["content":
    let ex2: [String: AnyObject] = ["key" : nonOptionalVar, "notherkey": ["content": optionalVar]]

    /*
    Without import UIKit both give
    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

    [Sratches head]
    */
  2. Steven Cassidy revised this gist Apr 17, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion optional-literal-gotcha.swift
    Original file line number Diff line number Diff line change
    @@ -13,5 +13,5 @@ let ex2: [String: AnyObject] = ["key" : nonOptionalVar, "notherkey": ["content":
    /*
    Without import UIKit both give
    error: value of type '[String]' does not conform to expected dictionary value type 'AnyObject'
    So UIKit seems to be doing something to affect interpretation of literals
    Sratches head
    */
  3. Steven Cassidy revised this gist Apr 17, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions optional-literal-gotcha.swift
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,17 @@
    import UIKit

    let nonOptionalVar: [String] = ["a", "b", "c"]

    let optionalVar:String? = "jimmy"

    // compiles OK with import UIKit
    // 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]]

    /*
    Without import UIKit both give
    error: value of type '[String]' does not conform to expected dictionary value type 'AnyObject'
    So UIKit seems to be doing something to affect interpretation of literals
    */

  4. Steven Cassidy revised this gist Apr 17, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions optional-literal-gotcha.swift
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,10 @@ let nonOptionalVar: [String] = ["a", "b", "c"]
    let optionalVar:String? = "jimmy"

    // compiles OK with import UIKit
    let ex1: [String: AnyObject] = ["hashtags" : nonOptionalVar, "tweet": ["content": optionalVar!]]
    let ex1: [String: AnyObject] = ["key" : nonOptionalVar, "notherkey": ["content": optionalVar!]]

    // error: contextual type 'AnyObject' cannot be used with dictionary literal
    let ex2: [String: AnyObject] = ["hashtags" : nonOptionalVar, "tweet": ["content": optionalVar]]
    let ex2: [String: AnyObject] = ["key" : nonOptionalVar, "notherkey": ["content": optionalVar]]

    /*
    Without import UIKit both give
  5. Steven Cassidy created this gist Apr 17, 2016.
    17 changes: 17 additions & 0 deletions optional-literal-gotcha.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    import UIKit

    let nonOptionalVar: [String] = ["a", "b", "c"]

    let optionalVar:String? = "jimmy"

    // compiles OK with import UIKit
    let ex1: [String: AnyObject] = ["hashtags" : nonOptionalVar, "tweet": ["content": optionalVar!]]

    // error: contextual type 'AnyObject' cannot be used with dictionary literal
    let ex2: [String: AnyObject] = ["hashtags" : nonOptionalVar, "tweet": ["content": optionalVar]]

    /*
    Without import UIKit both give
    error: value of type '[String]' does not conform to expected dictionary value type 'AnyObject'
    */