Reader small image

You're reading from  Swift Protocol-Oriented Programming - Fourth Edition

Product typeBook
Published inJun 2019
Reading LevelIntermediate
Publisher
ISBN-139781789349023
Edition4th Edition
Languages
Tools
Right arrow
Author (1)
Jon Hoffman
Jon Hoffman
author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon’s other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman

Right arrow

Extensions with the Swift standard library

Let's say that, in our application, we needed to calculate the factorial of some integers. A factorial is written as 5!. To calculate a factorial, we take the product of all the positive integers that are less than, or equal to, the number. The following example shows how we would calculate the factorial of five:

5!  =  5*4*3*2*1 
5!  =  120 

We could very easily create a global function to calculate the factorial, and we would do that in most languages. However, in Swift, extensions give us a better way to do this. The Integer type in Swift is implemented as a structure that we can extend to add this functionality directly to the type itself. The following example shows how we can do this:

extension Int { 
    func factorial() -> Int { 
        var answer = 1 
        for x in (1...self).reversed() { 
           answer *= x ...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Swift Protocol-Oriented Programming - Fourth Edition
Published in: Jun 2019Publisher: ISBN-13: 9781789349023

Author (1)

author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon’s other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman