How do I make this type optional?

let ability1? = dict[0] as! [String: AnyObject]
let ability2? = dict[1] as! [String: AnyObject]

Since you are dealing with JSON,

let ability1 = dict[0] as? [String: AnyObject]
let ability2 = dict[1] as? [String: AnyObject]

arg as! T - forced cast, returns T if it succeeds, otherwise fatalError.
arg as? T - conditional cast, returns T?, which is nil if the cast fails.

FYI: Type Casting

You can also use dict[0] as! [String: AnyObject]? to say "I know this is either nil or this specific dictionary type".

3 Likes