Posted on 2014.Jul.19, 8:21 pm By Jason
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
- Mine: The dictionary is a
String:Float
, so setting aString
value is wrong. - 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]()
“ - 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
Categories: Swift Tags: code, programmer, swift