Beginning Swift Programming. Lee Wei-Meng

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

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

Beginning Swift Programming - Lee Wei-Meng

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

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.

Figure 1.10

You should now see the iPhone Simulator appear (see Figure 1.11).

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.

Figure 1.12

SWIFT SYNTAX

      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 radius = 3.45

      let numOfColumns = 5

      let myName = "Wei-Meng Lee"

Notice that there is no need to specify the data type – they are inferred automatically. In the preceding example, radius is a Double, numOfColumns is an Int, while myName is a String. How can the programmer verify the variable type? A good way is to use Xcode’s Playground feature. Go ahead and type the preceding statements into your Playground project. Then, Option-click on each of the constants and look at the pop-up that appears. Figure 1.13 shows that the type of radius is Double.

Figure 1.13

      Readers familiar with Objective-C will immediately note the lack of the @ character when defining a string literal. In Objective-C, you need the @ character before a string:

      NSString *myName = @"Wei-Meng Lee"

      //-

      Objective-C-

      However, it is not needed in Swift: let myName = "Wei-Meng Lee"

      //-

      Swift-

      Also, in Objective-C you need to use the * to indicate memory pointers whenever you are dealing with objects; in Swift there is no need to use the *, regardless of whether you are using objects or primitive types.

       NOTE Strictly speaking, the String type in Swift is a primitive (value) type, whereas the NSString in Objective-C is a reference type (object). Strings are discussed in more detail in Chapter 3.

      If you wish to declare the type of constant, you can do so using the colon operator (:) followed by the data type, as shown here:

      let diameter:Double = 8

      The preceding statement declares diameter to be a Double constant. You want to declare it explicitly because you are assigning an integer value to it. If you don’t do this, the compiler will assume it is an integer constant.

      Once a constant is created, you can no longer change its value:

      let radius = 3.45radius = 5.67 //-error-

Figure 1-14 shows Playground flagging the statement as an error.

Figure 1.14

      Variables

      To declare a variable, you use the var keyword:

Figure 1.15

      let radius = 3.45 v

      ar myAge = 25

      var circumference = 2 * 3.14 * radius

      Once a variable is created, you can change its value:

      let diameter = 20.5

      circumference = 2 * 3.14 * diameter/2

Observe that after you type the preceding statements into Playground, the value of circumference is immediately computed and the result shown on the right (see Figure 1-15).

      In Swift, values are never implicitly converted to another type. For example, suppose you are trying to concatenate a string and the value of a variable. In the following example, you need to explicitly use the String() initializer to convert the value of myAge to a string value before concatenating it with another string:

      var strMyAge = "My age is " + String(myAge)

      //-My age is 25-

If you type the preceding statements into Playground, the value of strMyAge is immediately shown on the right (see Figure 1.16).

Figure 1.16

      Interestingly, an error will occur if you try to do something such as the following:

      var strCircumference =

      "Circumference of circle is " + String

      (circumference)

      This is because the String() initializer cannot convert the Double type (the circumference variable by type inference is Double) into a String type. To solve this, you need to use the string interpolation method, as described in the next section.

      NOTE You will learn more about data types in the next chapter.

      String Interpolation: Including Values in Strings

      One

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