//TODO: professional stuff of software engineer 1001010
value is not identical

Or: Fun with error messages!

Let‘s start with some crappy code I wrote in the Playground as I was going through the Swift language book.

let dict = [String:Float]()

dict
dict["Bob"] = 1.0
dict["Joe"] = "dude"
dict

Guess which dictionary insert line generated this error?
'@value $T5' is not identical to '(String, Float)' 

(insert Jeopardy suspenseful waiting music here)

If you’ve never written Swift I’m sure you’re probably at least partially right,
it was the “Joe” insertion

..and it was also the “Bob” insertion!

There are 3 problems here

  1. Mine: The dictionary is a String:Float, so setting a String value is wrong.
  2. Mine: The dictionary is “let” not “var“, so it’s read-only. Insertions are not allowed.
    The quick fix is to make it “var dict = [String:Float]()
  3. Swift: It’s a crappy error message. If it’s read only – then say it’s read only.

Dear Swift Language team at Apple,
When a user assigns a value to a read-only variable, please tell them root problem – not the immediate error message.  By being cryptic & indirect – you are reminding me of C++ template errors.

XOXO
-Jason De Arte

Swift Playground setting dict key values

About

Categories: Swift Tags: , ,