O'Reilly logo

iOS Programming Cookbook 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 extensions to extend classes functionality

For its name, extensions are used to extend an existing functionality. In Swift, you can extend classes, structures, protocols, and enumerations. Extensions are similar to categories in Objective-C except that extensions don't have names. It's very useful to add functionality to any type that you don't have access to its source code, such as native classes String, NSArray, and so on.

Getting ready

In Swift, syntax is pretty easy, and that's why it is awesome. To extend any type, just type the following:

extension TypeToBeExtended{ 
} 

Inside the curly braces, you can add your extensions to the type to be extended. In extension, you can do the following:

  • Adding instance- or class-computed properties
  • Adding ...

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