According to our explanation in the previous section about type casting and type checking, Swift provides us with two general types to use if you don't want to specifically type the objects:
These types are very useful, especially when you try to deal with Cocoa APIs, because, most of the time, you will receive an array of type AnyObject
. As we know, Objective-C doesn't support a typed array. In these situations, you have to use the is
, as
, and as?
operators of type casting and type checking to help you deal with Any
and AnyObject
types.
Let's see an example of using AnyObject
:
var anyObjects = ["Str", 5, true, UILabel(), NSDateFormatter()] anyObjects.append(11.5) anyObjects.append([1, 2])
As we have seen in this strange array, it holds String
, UILabel
, and NSDateFormatter...