9.4. Swift data type

发布时间 : 2025-10-25 13:33:50 UTC      

Page Views: 9 views

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

The data type of the variable determines how the bits that represent these values are stored in the computer’s memory. You can also specify its data type when you declare a variable.

All variables have data types to determine what kind of data can be stored.

9.4.1. Built-in data type #

Swift provides a wide range of data types, several of which are commonly used are listed below:

9.4.2. Int #

In general, you do not need to specify the length of an integer. Swift provides a special integer type Int the length is the same as the original word length of the current platform

  • On 32-bit platform Int and Int32 the length is the same.

  • On 64-bit platform Int and Int64 the length is the same.

Unless you need an integer of a specific length, generally use the Int .That’s enough. This can improve code consistency and reusability. Even on a 32-bit platform Int the range of integers that can be stored can also be reached -2,147,483,648 ~ 2,147,483,647 is big enough most of the time.

9.4.3. UInt #

Swift also provides a special unsigned type UInt the length is the sameas the original word length of the current platform

  • On 32-bit platform UInt and UInt32 the length is the same.

  • On 64-bit platform UInt and UInt64 the length is the same.

Note: try not to use UInt unless you really need to store an unsigned integer with the same length as the native word length of the current platform Except in this case, it is best to use Int even if the value you want to store is known to be non-negative Unified use Int can improve the reusability of the code, avoid the conversion between different types of numbers, and match the type inference of numbers.

Integer types need to be aware of the following:

  • 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 and Int64 represent 8-bit, 16-bit, 32-bit, and 64-bit signed integers, respectively.

  • UInt8, UInt16, UInt32, and UInt64 represent 8-bit, 16-bit, 32-bit and 64-bit unsigned integers, respectively.

9.4.4. Floating point numbers: Float, Double #

Floating point numbers are numbers with decimal parts, such as 3.14159, 0.1, and -273.15.

Floating-point types represent a wider range than integer types, and can store a larger range than Int a number with a larger or smaller type. Swift provides two types of signed floating point numbers:

  • Double represents a 64-bit floating-point number. Use this type when you need to store large or high-precision floating-point numbers.

  • Float represents a 32-bit floating point number. This type can be used if the precision requirement is not high.

Note: Double is very accurate, at least 15 digits, and Float there are at least six digits. Which type you choose depends on the range ofvalues your code needs to deal with.

9.4.5. Boolean value: Bool #

Swift has a basic Boolean type called Bool. Boolean values refer to logical values because they can only be true or false. Swift has two Boolean constants, true and false.

9.4.6. String: String #

A string is a sequence collection of characters, for example:

"Hello, World!" 

9.4.7. Character: #

A character refers to a single letter, such as:

"C" 

9.4.8. Optional type: #

Use optional types to handle situations where values may be missing. An optional type indicates that there is or no value.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。