Latest web development tutorials

Swift Data Types

When we use any programming language programming, you need to use a variety of data types to store different information.

The data type of the variable determines how bit memory these values ​​into the computer's memory. You can also specify its data type when you declare a variable.

All the variables have the data type to determine which data can be stored.


Built-in data types

Swift provides a very rich data types, here are several commonly used data types:

Int

Generally, you do not need to specifically designated length integers. Swift provides a special integer type Int , the length of the original vocabulary and the same current platform length:

  • On 32-bit platforms, Int and Int32 same length.
  • On 64-bit platforms, Int and Int64 same length.

Unless you need a specific length integer, in general use Int enough. This can improve the consistency and reusability of code. Even on 32-bit platforms, Int integer range that can be stored can be reached -2,147,483,648 ~ 2,147,483,647 , most of the time this is already large enough.

UInt

Swift also offers a special unsigned type UInt , the length of the original vocabulary and the same current platform length:

  • On 32-bit platforms, UInt and UInt32 same length.
  • On 64-bit platforms, UInt and UInt64 same length.

note:
Try not to use UInt , unless you really need to store a current platform and original vocabulary of the same length unsigned integer. In addition to this, it is preferable to use Int , even if you want to store the value of a known non-negative. Unified use Int can improve code reusability, to avoid the conversion between different types of digital, and digital matching type inference, refer to the type of security and type inference .

Float

Float is a number with a decimal portion, such as 3.14159 , 0.1 and -273.15 .

Wider than the floating-point type representation of integer type that can store more than Int larger or smaller type numbers. Swift offers two signed floating-point type:

  • Double represent 64-bit floating point. When you need to store large or very high-precision floating-point number, please use this type.
  • Float represents 32-bit floating-point number. Less precision, then we can use this type.

note:
Double precision is very high, at least 15 digits, Float happened only six digits. Select the range of values ​​depending on which type your code need to be addressed.

Boolean value

Swift has a basic boolean (Boolean) type, called Bool. Boolean value refers to the value logic, because they can only be true or false. Swift has two Boolean constant, true and false.

String

String is a set of sequences of characters, such as:

"Hello, World!"

character

Character refers to a single letter, for example:

"C"

Optional type

Use the optional type (optionals) to handle the case may be missing values. Optional type represents a value or no value.


Value range

The following table shows the minimum and maximum storage space of different types of memory variables and variable types:

Types of Size (bytes) Interval Value
Int8 1 byte -127 To 127
UInt8 1 byte 0-255
Int32 4 bytes -2147483648 To 2147483647
UInt32 4 bytes 0-4294967295
Int64 8 bytes -9223372036854775808 To 9223372036854775807
UInt64 8 bytes 0-18446744073709551615
Float 4 bytes 1.2E-38 to 3.4E + 38 (~ 6 digits)
Double 8 bytes 2.3E-308 to 1.7E + 308 (~ 15 digits)

Type alias

Type an alias for the current type defines another name, type the alias defined by using typealias keyword. Syntax is as follows:

typealias newname = type

The following example defines the Int type alias Feet:

typealias Feet = Int

Now, we can define a variable alias:

import Cocoa

typealias Feet = Int
var distance: Feet = 100
print(distance)

We use the playground the above program, the output is:

100

Type Security

Swift is a type of security (type safe) language.

Because Swift is type-safe, so it will be the type of inspection (type checks) at compile your code, and do not match the type of mark as an error. This allows you to find and fix errors early in the development of the time.

import Cocoa

var varA = 42
varA = "This is hello"
print(varA)

The above procedure will be given in Xcode:

error: cannot assign value of type 'String' to type 'Int'
varA = "This is hello"

Meaning can not be 'String' string assigned to 'Int' variable.


Type inference

When you have to deal with different types of values, type checking can help you avoid mistakes. However, this does not mean that every time you declare constants and variables need to explicitly specify the type.

If you do not explicitly specify the type, Swift uses the type inference (type inference) to select the appropriate type.

For example, if you give a new constant values ​​42 and does not indicate the type, Swift can infer constant type Int, because the initial value you assign it looks like an integer:

let meaningOfLife = 42
// meaningOfLife 会被推测为 Int 类型

Similarly, if you do not have to indicate the type of floating-point literals, Swift will infer you want is Double:

let pi = 3.14159
// pi 会被推测为 Double 类型

When a floating-point type inference, Swift will always choose instead of Double Float.

If the same expression as integer and floating-point, will be inferred Double type:

let anotherPi = 3 + 0.14159
// anotherPi 会被推测为 Double 类型

The original value of 3 does not explicitly declare the type, and the expression there is a floating-point literal, the expression will be presumed to Double type.

Examples

import Cocoa

// varA 会被推测为 Int 类型 
var varA = 42
print(varA)

// varB 会被推测为 Double 类型  
var varB = 3.14159
print(varB)

// varC 也会被推测为 Double 类型   
var varC = 3 + 0.14159
print(varC)

Implementation of the above code, the output is:

42
3.14159
3.14159