Beginning Swift Programming. Lee Wei-Meng

Читать онлайн.
Название Beginning Swift Programming
Автор произведения Lee Wei-Meng
Жанр Зарубежная образовательная литература
Серия
Издательство Зарубежная образовательная литература
Год выпуска 0
isbn 9781119009320



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

The compiler ignores comments at compilation time.

      If you have several lines of comments, it is better to use the /* and */ combination to denote a block of statements as comments. For example:

      /* this is a comment

      this is another comment

      */

      The two preceding lines are marked as a comment.

      You can also nest comments, as shown in the following example:

      // this is a comment

      var myAge = 25

      var circumference = 2 * 3.14 * radius

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

      /* this is a comment

      this is another comment

      */

      println(strMyAge)

      To comment the entire block of code, enclose everything within the /* and */, as shown here:

      /*// this is a comment

      var myAge = 25

      var circumference = 2 * 3.14 * radius

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

      /*

      this is a comment

      this is another comment

      */

      println(strMyAge)

      */

       NOTE In other languages such as C and Java, you are not allowed to nest comments.

SUMMARY

      In this chapter, you learned about Apple’s motives for creating Swift, as well as how to obtain the tools to start learning it. You also had a brief look at its syntax. If you have been an Objective-C developer until now, your first impression of Swift is likely a positive one, as it is a thoroughly contemporary and safe language, without the obscure syntax of Objective-C. In the following chapters, you will learn about various other impressive aspects of Swift.

      EXERCISES

      1. Declare three constants: to store the number of months in a year, the number of days in a week, and the number of weeks in a year.

      2. Declare variables to store a user’s gender, weight, height, and date of birth.

      3. Write statement(s) to print out the details of the user using the variables that you have declared in question #2.

      4. The following statements resulted in a compiler error. Fix it.

      var weight = 102.5 // in poundsvar str = "Your weight is " + weight + " pounds"

      

WHAT YOU LEARNED IN THIS CHAPTER

      2

      Data Types

      WHAT YOU WILL LEARN IN THIS CHAPTER:

      • The basic data types: integers, floating-point numbers, and Booleans

      • The types of integers

      • How to perform integer operations

      • Different ways to represent integer literals

      • The two different floating-point types

      • How to perform floating-point operations

      • Different ways to represent floating-point literals

      • How to create type aliases

      • What tuples are

      • The new optional types

      • How to declare implicitly unwrapped optionals

      • How to perform optional binding

      • How to unwrap optionals using the? character

      • How to define enumerations

      In Chapter 1, you took a quick look at the syntax of Swift statements, as well as how to declare variables and constants quickly using type inference. In this chapter, you will learn more about the various data types available in the language.

      In addition to supporting the various basic data types available in most programming languages, Swift also introduces new data types not available in Objective-C. Such new data types include the following:

      • Tuples– A tuple is a group of related values that can be manipulated as a single data type. Tuples are very useful when you need to return multiple values from a function.

      • Optional types– An optional type specifies a variable that can contain no value. Optional types make your code safer, as you will learn later in this chapter.

      Swift is a type-safe language. 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 are not allowed to be used in a statement and will be flagged as errors.

BASIC DATA TYPES

      Like most programming languages, Swift provides the following basic data types:

      • Integers

      • Floating-point numbers

      • Booleans

      Integers

      Integers are whole numbers with no fractional parts. Integers can be positive or negative. In Swift, integers are represented using the Int type. The Int type represents both positive as well as negative values. If you only need to store positive values, you can use the unsigned integer UInt type. The size of an Int type depends on the system on which your code is running. On 32-bit systems, Int and UInt each use 32 bits for storage, whereas on 64-bit systems Int and UInt each use 64 bits.

      You can programmatically check the number of bytes stored by each data type using the sizeof() function:

      println("Size of Int: \(sizeof(Int)) bytes")

      println("Size of UInt: \(sizeof(UInt)) bytes")

      If you run the preceding statement on an iPhone 5 (which uses the 32-bit A6 chip), you will get the following:

      Size of Int: 4 bytes

      Size of UInt: 4 bytes

      If you run the preceding statement on an iPhone 5s (which uses the 64-bit A7 chip), you will get the following:

      Size of Int: 8 bytes

      Size of UInt: 8 bytes

      If you do not know the type of data a variable is storing, you can use the sizeofValue() function:

      var num = 5

      println("Size of num: \(sizeofValue(num)) bytes")

      Types of Integers

      In most cases, you will use Int for storing signed numbers, and UInt if you do not need to store negative values (even if you don’t need to store negative numbers it is still a good idea to use Int for code compatibility). However, if you want to explicitly control the size of the variable used, you can specify one of the various integer types available:

      • Int8 and UInt8

      • Int16 and UInt16

      • Int32 and UInt32

      • Int64 and UInt64

       NOTE On 32-bit systems, Int is the same as Int32, while on 64-bit systems, Int is the same as Int64.

       On 32-bit systems, UInt is the same as UInt32, while on 64-bit systems, UInt is the same as UInt64.

      The following code snippet prints the range of numbers representable for each integer type:

      //-UInt8 – Min: 0 Max: 255-println("UInt8 – Min: