Not so easy as you might think, in fact. It's is a bit messy since you need to handle the fact that when the key isn’t present, you need to initialize the array. Here’s one way of doing it (code altered to be similar but stand-alone):
var dictionary = [String: [String]]()
let courses = ["Thing","Other Thing","Third Thing"]
for course in courses {
// note, using `first` with `if…let` avoids a crash in case
// you ever have an empty course title
if let firstChar = first(course).map({String($0).uppercaseString}) {
// get out the current values
let current = dictionary[firstChar]
// if there were none, replace nil with an empty array,
// then append the new entry and reassign
dictionary[firstChar] = (current ?? []) + [course]
}
}
Alternatively, if you want to use .append you could do this:
// even though .append returns no value i.e. Void, this will
// return Optional(Void), so can be checked for nil in case
// where there was no key present so no append took place
if dictionary[firstChar]?.append(course) == nil {
// in which case you can insert a single entry
dictionary[firstChar] = [course]
}