Stupid Swift error message #a bajillion and one

Input code:

let componentsOfPotentialInterest = [Calendar.Component: ((Int) -> String)](
 .day: { String($0 + 1) },
)

Push button.  Expect results (or at least bacon).  Get:

Foo.swift:76:21: error: expected ',' separator
 .day: { String($0 + 1) },
     ^
     ,
Foo.swift:76:21: error: expected expression in list of expressions
 .day: { String($0 + 1) },
     ^
Foo.swift:76:21: error: expected ',' separator
 .day: { String($0 + 1) },
     ^
     ,

Believe it or not, Swift, blindly repeating your obtuse error messages does not help.

What it’s trying but as usual failing miserably to tell me is that Dictionary doesn’t have an initialiser that takes key: value pairs (my mistake for writing straight-forward, Python-like code).  You have to use the dictionary literal syntax instead:

let componentsOfPotentialInterest: [Calendar.Component: ((Int) -> String)] = [
 .day: { String($0 + 1) },
]

Now it merely complains about the expression being too complex for its pathetic little brain, rather than having no fucking clue what you’re doing to begin with.  Pick your utterly useless poison, I suppose.

Leave a Comment