O'Reilly logo

Application Development with Swift by Hossam Ghareeb

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Any and AnyObject

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:

  • AnyObject: This type is used to represent instances of class types only and its equivalent to id in Objective-C
  • Any: This type is used to represent instances of any type like tuples, closures, and so on

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required