Beginning Swift Programming. Lee Wei-Meng

Чтение книги онлайн.

Читать онлайн книгу Beginning Swift Programming - Lee Wei-Meng страница 3

Beginning Swift Programming - Lee Wei-Meng

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

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).

Figure 1.1

Once Xcode 6 is downloaded and installed on your Mac, launch it (see Figure 1.2).

Figure 1.2

      There are two ways to test the code in this book:

      • Create a Playground project– Playground is a new feature in Xcode 6 that makes learning Swift easy and fun. As you enter each line of code, Playground will evaluate the line and display the results. You can also use it to watch the values of variables as you step through the code. Playground is very useful for examining variable types when you are assigning values to them.

      • Create an iOS project– You can create an iOS project and test your application using the iPhone Simulator included in the Xcode 6. While the focus of this book is on the Swift programming language and not iOS development, testing your code in an iOS project enables you to test your code in its entirety.

      Creating a Playground Project

To create a Playground project, launch Xcode 6 and select File

New
Playground… Name the Playground project and select the platform you want to test it on (see Figure 1.3).

Figure 1.3

Once the Playground project is created, you will see the editor shown in Figure 1.4. You can start writing your Swift code in this editor. I will show you some of Playground’s neat features as we discuss the various Swift topics covered in this chapter.

Figure 1.4

      For example, consider the following code snippet:

      var sum = 0

      for index in 1…5 {

      sum += index

      }

The preceding code snippet sums all the numbers from 1 to 5. If you type this code snippet into Playground, you will see that the right side of the Playground window displays a circle (see Figure 1.5).

Figure 1.5

Clicking on the circle will reveal the Timeline, where you can examine the values for sum for each iteration of the For loop (see Figure 1.6).

Figure 1.6

      This feature makes it very easy for you to trace through your code, and it is especially useful when you are analyzing your new algorithm.

      NOTE The For loop is discussed in more detail in Chapter 7.

      Creating an iOS Project

An alternative to creating a Playground project is to create an iOS project. In Xcode 6, select File

New
Project… and you will see the dialog shown in Figure 1.7.

Figure 1.7

      Select Application under the iOS category (on the left) and then select the Single View Application template. Click Next.

      NOTE The Single View Application template creates an iPhone project with a single View window. This is the best template to use for learning Swift without getting bogged down with how an iOS application works.

In the next dialog, enter the information as follows (see Figure 1.8):

      • Product Name– The name of the project.

      • Organization Name– This can either be your name or your organization’s name.

      • Organization Identifier– Commonly the reverse domain name of your company. If your organization’s domain name were example.com, then you would enter com.example. The Organization Identifier and the Product Name are concatenated to form a unique string called the Bundle Identifier. Every application listed on the App Store must have a unique Bundle Identifier. For testing purposes, this is not important.

      • Language– Select Swift.

      • Devices– Select iPhone.

Figure 1.8

Once the information is entered, click Next and select a location to save the project, and then click Create. Xcode will proceed to create the project. In the created project, select the ViewController.swift file for editing (see Figure 1.9).

Figure 1.9

      To test your Swift code, you can insert it in the position indicated in bold in the following example:

      import

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