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

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 of the dreaded tasks in Objective-C is inserting values of variables in a string. (You have to use the NSString class and its associated stringWithFormat: method to perform string concatenation, which makes your code really long.)

      In Swift, this is very easy using the \() syntax, known as string interpolation. It has the following format:

      "Your string literal \(variable_name)"

      The following example shows how:

      let myName = "Wei-Meng Lee"

      var strName = "My name is \(myName)"

      You can use this method to include a Double value in your string as shown here:

      var strResult = "The circumference is \

      (circumference)"

      Statements

      You might have noticed that in the statements you wrote earlier, unlike most other programming languages each statement does not end with a semicolon (;):

      let radius = 3.45

      let numOfColumns = 5

      let myName = "Wei-Meng Lee"

      If you want to include semicolons at the end of each statement, it is syntactically correct but not necessary:

      let radius = 3.45; let numOfColumns = 5; let myName = "Wei-Meng Lee";

      The only time the semicolon is required is when you combine multiple statements into one single line:

      let radius = 3.45; let numOfColumns = 5; let myName = "Wei-Meng Lee";

      Printing

      You can print the current values of variables or constants using the println() or print() function. The print() function prints out the value, whereas the println() function does the same and additionally prints a line break. These two functions are similar to Cocoa’s NSLog function (for readers who are familiar with Objective-C).

      In Playground, the println() and print() functions print the values to the Console Output window in the Timeline; in Xcode, these functions print out the values to the Output window. The following statements print out the value of strMyAge:

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

Figure 1.17 shows the output of the preceding statements in Xcode’s Output window. (Press Command+Shift+C to reveal the Output window.)

Figure 1.17

      Comments

      In Swift, as in most programming languages, you insert comments into your code using two forward slashes (//):

      // this is a comment

      // this is another comment

      The

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