Lee Wei-Meng

Beginning Swift Programming


Скачать книгу

to each book’s errata, is also available at www.wrox.com/misc-pages/booklist.shtml.

      If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

P2P.WROX.COM

      For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and related technologies and to interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

      At p2p.wrox.com, you will find a number of different forums that will help you not only as you read this book but also as you develop your own applications. To join the forums, just follow these steps:

      1. Go to p2p.wrox.com and click the Register link.

      2. Read the terms of use and click Agree.

      3. Complete the required information to join as well as any optional information you want to provide and click Submit.

      4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

      NOTE You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

      After you join, you can post new messages and respond to messages that other users post. You can read messages at any time on the web. If you want to have new messages from a particular forum e-mailed to you, click the Subscribe to This Forum icon by the forum name in the forum listing.

      For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as for many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

      1

      Introduction to Swift

      WHAT YOU WILL LEARN IN THIS CHAPTER:

      • What Swift is

      • Why Swift is important

      • Setting up the development environment to learn Swift

      • How to create a Playground project

      • How to create an iOS project

      • The syntax of Swift

      • How to declare constants

      • How to declare variables

      • Using string interpolation to include variable values in strings

      • Swift statements

      • How to print the values of variables for debugging

      • How to insert comments in your Swift code

      Apple surprised the Mac and iOS developer world at the Apple World Wide Developers Conference (WWDC) 2014 with the announcement of a new programming language: Swift. The aim of Swift is to replace Objective-C with a much more modern language syntax without worrying too much about the constraints of C compatibility. Apple itself touted Swift as Objective-C without the C.

      For developers already deeply entrenched in Objective-C, it is foreseeable that Objective-C will still be the supported language for iOS and Mac OS X development in the near and immediate future. However, signs are all pointing to Apple’s intention to make Swift the future language of choice for iOS and Mac development.

      In this chapter, you will learn about the basics of Swift and how you can set up the development environment to learn it.

WHAT IS SWIFT?

      Swift is a new programming language designed by Apple for Cocoa (Mac OS X) and Cocoa Touch (iOS) programming. The syntax of Swift is similar to modern languages such as Java and C#, while at the same time retaining some of the core features of Objective-C, such as named parameters, protocols, and delegates. The language’s clear syntax makes your code simpler to read and maintain.

      As an example, consider the following method in Objective-C:

      -(int) addOneNumber:(int) num1 withAnotherNum:(int) num2{ return num1 + num2;}

      The preceding method adds two numbers and returns their sum. To use the method, you can pass a message to it:

      int sum = [self addOneNumber:2 withAnotherNum:7];

      Note the verbosity of Objective-C and the use of named parameters in the method name. The following example shows the same method in Swift:

      func addTwoNumbers(num1:Int, num2:Int) – > Int { return num1 + num2}

      The preceding statements define a function called addTwoNumbers, accept two arguments, and return an integer value. You can call the method like this:

      var sum = addTwoNumbers(2,5)

      As you can see, Swift’s syntax is simpler and easier to read.

      In keeping with Objective-C’s named parameters tradition, you can also use named parameters in methods:

      func addTwoNumbers(num1:Int, secondNumber num2:Int) – > Int { return num1 + num2}

      You can now call the method using named parameters:

      var sum = addTwoNumbers(2, secondNumber:5)

       NOTE Chapter 5 discusses functions and named parameters in more detail.

      Swift is also designed to be a type-safe language. Variables must be initialized before use. In most cases, you have to perform explicit type conversions when assigning values from one type to another. Also, variables that are not assigned a value cannot be used in a statement and will be flagged as errors.

      In Swift, for safety reasons there is no implicit type conversion – you must explicitly convert an Int to a Float (or Double). For example, you cannot implicitly assign an Int variable to a Float variable:

      var f: Floatvar i: Int = 5f = i //-error-

      Rather, you need to explicitly convert the value into a Float value:

      f = Float(i)

       NOTE Chapter 2 discusses data types in more detail.

WHY SWIFT IS IMPORTANT

      Make no mistake; Apple did not create Swift for the sake of creating a new programming language. With the platform wars heating up, Apple desperately needs a language that will enable it to secure its long-term lead in the mobile platform market. Swift is strategic to Apple in a number of ways:

      • It fixes many of the issues developers had with Objective-C – particularly, that Objective-C is hard to learn – replacing it with a language that is both fast to learn and easy to maintain.

      • It delivers this easy-to-learn language while retaining the spirit of Objective-C but without its verbose syntax.

      • It is a much safer language than Objective-C, which contributes to a much more robust app platform.

      • It is able to coexist with Objective-C, which gives developers ample time to port their code to Swift over time.

SETTING UP THE ENVIRONMENT

To test all the Swift examples in this book, you need a Swift compiler. The easiest way to obtain the Swift compiler is to download the Xcode 6 from the Mac App Store (see Figure 1.1).

images