ТОП просматриваемых книг сайта:
Beginning Swift Programming. Lee Wei-Meng
Читать онлайн.Название Beginning Swift Programming
Год выпуска 0
isbn 9781119009320
Автор произведения Lee Wei-Meng
Жанр Зарубежная образовательная литература
Издательство John Wiley & Sons Limited
Once Xcode 6 is downloaded and installed on your Mac, launch it (see 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).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.
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).
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).
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.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.
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).
To test your Swift code, you can insert it in the position indicated in bold in the following example:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//-insert your Swift code here- println("Hello, Swift!")
// Do any additional setup after loading the view, typically from a // nib.}
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated. }}
To run the application, select the iPhone 6 Simulator and click the Build and Run button (see Figure 1.10). Alternatively, you can also use the Command+R keyboard shortcut.
You should now see the iPhone Simulator appear (see Figure 1.11).
As our focus in this book is not on iOS programming, you would be primarily interested in the output generated by your Swift code. Back in Xcode 6, press Command+Shift+C to reveal the Output window. Figure 1.12 shows our single Swift code printing out a line in the Output window.
Now that you know how to set up the development environment for learning Swift and have looked at the various types of projects you can create to experiment with it, this section introduces the various syntaxes of Swift, beginning with how to create constants and variables.
Constants
In Swift, you create a constant using the let keyword:
let