YouTip LogoYouTip

Swift Data Types

When programming in any programming language, you need to use various data types to store different information. The data type of a variable determines how the bits representing these values are stored in the computer's memory. You can also specify its data type when declaring a variable. All variables have a data type to determine what kind of data they can store. * * * ## Built-in Data Types Swift provides a very rich set of data types. The following lists several commonly used data types: ### Int Generally, you do not need to specifically specify the length of an integer. Swift provides a special integer type `Int`, which has the same length as the native word size of the current platform: * On 32-bit platforms, `Int` is the same size as `Int32`. * On 64-bit platforms, `Int` is the same size as `Int64`. Unless you need an integer of a specific length, `Int` is generally sufficient. This improves code consistency and reusability. Even on 32-bit platforms, the range of integers `Int` can store reaches `-2,147,483,648` to `2,147,483,647`, which is large enough most of the time. ### UInt Swift also provides a special unsigned type `UInt`, which has the same length as the native word size of the current platform: * On 32-bit platforms, `UInt` is the same size as `UInt32`. * On 64-bit platforms, `UInt` is the same size as `UInt64`. > **Note:** Try to avoid using `UInt` unless you specifically need to store an unsigned integer with the same length as the current platform's native word size. Other than this, it is best to use `Int`, even if the value you want to store is known to be non-negative. Consistently using `Int` improves code reusability, avoids conversions between different numeric types, and matches numeric type inference. Note the following points about integer types: * On 32-bit systems, Int and Int32 are the same length. * On 64-bit systems, Int and Int64 are the same length. * On 32-bit systems, UInt and UInt32 are the same length. * On 64-bit systems, UInt and UInt64 are the same length. * Int8, Int16, Int32, Int64 represent 8-bit, 16-bit, 32-bit, and 64-bit signed integer forms, respectively. * UInt8, UInt16, UInt32, UInt64 represent 8-bit, 16-bit, 32-bit, and 64-bit unsigned integer forms, respectively. ### Floating-point numbers: Float, Double Floating-point numbers are numbers with a fractional part, such as `3.14159`, `0.1`, and `-273.15`. Floating-point types represent a larger range than integer types and can store numbers larger or smaller than what the `Int` type can store. Swift provides two signed floating-point number types: * **Double** represents a 64-bit floating-point number. Use this type when you need to store floating-point numbers that are very large or require high precision. * **Float** represents a 32-bit floating-point number. Use this type if high precision is not required. > Note: > > `Double` has high precision, with at least 15 decimal digits, whereas `Float` has a minimum of only 6 decimal digits. Which type to choose depends on the range of values your code needs to handle. ### Boolean: Bool Swift has a basic Boolean type called Bool. Boolean values refer to logical values, as they can only be true or false. Swift has two Boolean constants, true and false. ### String: String A string is a collection of characters, for example: "Hello, World!" ### Character: Character A character refers to a single letter, for example: "C" ### Optional Type: Optional Use optional types to handle situations where a value may be missing. An optional type represents either a value or no value. * * * ## Numeric Ranges The following table shows the storage space in memory for different variable types, as well as the maximum and minimum values for these variable types: | Type | Size (Bytes) | Range | | --- | --- | --- | | Int8 | 1 byte | -128 to 127 | | UInt8 | 1 byte | 0 to 255 | | Int32 | 4 bytes | -2147483648 to 2147483647 | | UInt32 | 4 bytes | 0 to 4294967295 | | Int64 | 8 bytes | -9223372036854775808 to 9223372036854775807 | | UInt64 | 8 bytes | 0 to 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 Aliases A type alias defines another name for an existing type. Type aliases are defined using the typealias keyword. The syntax format is as follows: typealias newname = type For example, the following defines a type alias Feet for Int: typealias Feet = Int Now, we can define variables using the alias: import Cocoa typealias Feet = Intvar distance: Feet = 100print(distance) When we execute the above program in a playground, the output is: 100 * * * ## Type Safety Swift is a type safe language. Because Swift is type safe, it performs type checks when compiling your code and flags mismatched types as errors. This allows you to catch and fix errors early during development. import Cocoavar varA = 42 varA = "This is hello"print(varA) The above program will produce an error in Xcode: error: cannot assign value of type 'String' to type 'Int' varA = "This is hello" This means you cannot assign a 'String' value to an 'Int' variable. * * * ## Type Inference When you want to work with values of different types, type checking can help you avoid errors. However, this does not mean you have to explicitly specify the type every time you declare a constant or variable. If you do not explicitly specify the type, Swift will use type inference to choose the appropriate type. For example, if you assign a value of 42 to a new constant without indicating its type, Swift can infer that the constant's type is Int, because the initial value you assigned looks like an integer: let meaningOfLife = 42// meaningOfLife is inferred to be of type Int Similarly, if you do not indicate the type of a floating-point literal, Swift will infer that you want a Double: let pi = 3.14159// pi is inferred to be of type Double When inferring the type of a floating-point number, Swift always chooses Double over Float. If both an integer and a floating-point number appear in an expression, the type will be inferred as Double: let anotherPi = 3 + 0.14159// anotherPi is inferred to be of type Double The original value 3 does not have an explicitly declared type, and a floating-point literal appears in the expression, so the expression is inferred to be of type Double. ### Example import Cocoa// varA is inferred to be of type Int var varA = 42print(varA)// varB is inferred to be of type Double var varB = 3.14159print(varB)// varC is also inferred to be of type Double var varC = 3 + 0.14159print(varC) Executing the above code, the output result is: 423.141593.14159
← Swift VariablesSwift Basic Syntax β†’