Читать книгу Beginning Swift Programming - Lee Wei-Meng - Страница 3

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: \(UInt8.min) Max: \(UInt8.max)")

//-UInt16 – Min: 0 Max: 65535-println("UInt16 – Min: \(UInt16.min) Max: \(UInt16.max)")

//-UInt32 – Min: 0 Max: 4294967295-println("UInt32 – Min: \(UInt32.min) Max: \(UInt32.max)")

//-UInt64 – Min: 0 Max: 18446744073709551615-println("UInt64 – Min: \(UInt64.min) Max: \(UInt64.max)")

//-Int8 – Min: -128 Max: 127-println("Int8 – Min: \(Int8.min) Max: \(Int8.max)")

//-Int16 – Min: -32768 Max: 32767-println("Int16 – Min: \(Int16.min) Max: \(Int16.max)")

//-Int32 – Min: -2147483648 Max: 2147483647-println("Int32 – Min: \(Int32.min) Max: \(Int32.max)")

//-Int64 – Min: -9223372036854775808 Max: 9223372036854775807-println("Int64 – Min: \(Int64.min) Max: \(Int64.max)")

For each integer type, the min property returns the minimum number representable and the max property returns the maximum number representable.

Integer Operations

When you try to add two numbers of different integer types, you will get an error. Consider the following example:

var i1: UInt8 = 255

var i2: UInt16 = 255

var i3 = i1 + i2 //-cannot add two variables

of different types-

To fix this, you need to typecast one of the types to be the same as the other type:

var i3 = UInt16(i1) + i2 //-i3 is now UInt16-

Integer Literals

You can represent integer values as follows:

• Decimal

• Binary – Use the 0b prefix.

• Octal – Use the 0o prefix.

• Hexadecimal – Use the 0x prefix.

The following code snippet shows the number 15 represented in the four forms:

let num1 = 15 //-decimal-

let num2 = 0b1111 //-binary

let num3 = 0o17 //-octal-

let num4 = 0xF //-hexadecimal-

You can pad the integers with zeros if you want to make them more readable. The preceding code snippet can be rewritten as the following statements without changing the value represented:


Конец ознакомительного фрагмента. Купить книгу
Beginning Swift Programming

Подняться наверх