I have observed that it is possible to use the subscript operator with AnyObject
instances in Swift. For example:
let x: AnyObject = NSDictionary()
print(type(of: x["y"]))
This code produces the following output:
ImplicitlyUnwrappedOptional<Optional<Any>>
The first part makes sense, because invoking any method on an instance of AnyObject
is defined to behave like an implicitly unwrapped optional. The rest of the output suggests that the subscript operator itself is defined to return Optional<Any>
, which also makes sense, since it parallels the behavior of subscripts for dictionary instances.
I assume this is by design, but I haven't been able to verify it. Does anyone know if the behavior of the subscript operator for AnyObject
is documented anywhere?