Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Application Development with Swift
  • Table Of Contents Toc
Application Development with Swift

Application Development with Swift

By : Hossam Ghareeb
4.6 (5)
close
close
Application Development with Swift

Application Development with Swift

4.6 (5)
By: Hossam Ghareeb

Overview of this book

After years of using Objective-C for developing apps for iOS/Mac OS, Apple now offers a new, creative, easy, and innovative programming language for application development, called Swift. Swift makes iOS application development a breeze by offering speed, security and power to your application development process. Swift is easy to learn and has awesome features such as being open source, debugging,interactive playgrounds, error handling model, and so on. Swift has simplified its memory management with Automatic Reference Counting (ARC) and it is compatible with Objective-C. This book has been created to provide you with the information and skills you need to use the new programming language Swift. The book starts with an introduction to Swift and code structure. Following this, you will use playgrounds to become familiar with the language in no time. Then the book takes you through the advanced features offered by Swift and how to use them with your old Objective-C code or projects. You will then learn to use Swift in real projects by covering APIs such as HealthKit, Metal, WatchKit, and Touch ID in each chapter. The book's easy to follow structure ensures you get the best start to developing applications with Swift.
Table of Contents (9 chapters)
close
close

Functions

In Swift, it's very easy to create the functions using the func keyword, followed by the function name and parameters between the parentheses. Parentheses are required even if the function doesn't take parameters. So we will use parentheses without writing anything in between. For some other languages we need to use the void keyword for the function that doesn't take parameters. Parameters are comma separated and each one is combined with two items: the name and the type of parameters, separated by : (colon). Check this example of a simple function:

func sayHi(name:String)
{
    println("Hi \(name)")
}

sayHi("John") //Hi John

One of the great features that I love in Objective-C is that you can write a full description for each parameter in methods, which helps everyone, working in the project, to understand the code. The same is here in Swift; you can label parameters more easily. To do so, just type the name (description) before the parameter, or add # before the name of parameter, if the name is sufficient as a description and meaningful. Check these two examples:

func joinStrings(string1 str1:String, toString str2:String, joinWith j:String)
{
    println(str1 + j + str2)
}

joinStrings(string1: "John", toString: "Smith", joinWith: ",") //"John,Smith"

func joinStrings2(#string1:String, #toString:String, #glue:String)
{
    println(string1 + glue + toString)
}

joinStrings2(string1: "John", toString: "Deo",glue: "/") //"John/Deo"

As we saw in the first method, we wrote the description for each parameter, so that the code would be readable and understandable. In the second one, the parameter names were self-explained, and they needed no explanation. In such a case, we just added # before them, and then the compiler treated them as labels for parameters.

Like any function in any language, Swift may return results after the execution. Functions can return multiple values and not just a value like Objective-C, thanks to tuples! To do this, just write -> after the parentheses, and then add a tuple that describes the results:

func getMaxAndSum(arr:[Int]) -> (max:Int, sum:Int)
{
    var max = Int.min
    var sum = 0
    for num in arr{
        if num > max
        {
            max = num
        }
        sum += num
    }
    return(max, sum)
}

let result = getMaxAndSum([10, 20, 5])
result.max //20
result.sum //35
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Application Development with Swift
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon