When accessing a dictionary, such as [String: Any], the result type is Optional(Any).
When indexing an array of [Any], the result type is Any, and the call can throw a fatal error.
Is there any reason for this difference?
It would be so nice to branch execution with a guard let, if let, ?, and ??, but instead you have to wrap array indexing in an if data.count <= index.
throw, it raises an exception. An out-of-range exception cannot be caught withtry - catch.subscript (index: Index) -> Element?method, but then you have to call it aslet b: Int? = a[0]orlet b = a[0] as Int?so that the compiler can distinguish the methods from the context. I would not recommend that. – Actually I would not use a “safe subscript” at all. As said above, you always know which indices are valid. A failure might indicate a logic error in your code.