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

Extensions

Extensions are used to add new functionality to an existing class, enumeration, or structure. They are like categories in Objective-C, but in Swift we have two differences:

  • Extensions can be used with classes, enumerations, and structures
  • Extensions don't have names

In Swift, extensions can do many things. Check this list:

  • Add computed properties and computed type properties
  • Add instance methods and class methods
  • Define subscripts
  • Add new initializers
  • Make the existing type conform to protocol

To create an extension, use this form:

extension someType{
    //New functionalities go here
}

In extensions, as we said, you can add new functionalities, but you can't override an existing one.

Adding computed properties

Extensions can add computer instance properties ...

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