Swift has a variety of basic data types to represent different kinds of values. Here's a table summarizing them, along with their typical sizes and value ranges:
32-bit Int: -2,147,483,648 to 2,147,483,647
64-bit Int: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Data Type | Description | Size (bits) | Range/Values | Example |
---|---|---|---|---|
Numeric | ||||
Int | Signed integer (platform dependent) | 32 or 64 | -2^31 to (2^31)-1 (32-bit) -2^63 to (2^63)-1 (64-bit) | 10 , -5 , 1234567890 |
Int8 | Signed integer | 8 | -128 to 127 | 10 , -5 |
Int16 | Signed integer | 16 | -32768 to 32767 | 10000 , -2000 |
Int32 | Signed integer | 32 | -2,147,483,648 to 2,147,483,647 | 123456789 , -1000000 |
Int64 | Signed integer | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 1000000000000 , -5000000000 |
UInt | Unsigned integer (platform dependent) | 32 or 64 | 0 to (2^32)-1 (32-bit) 0 to (2^64)-1 (64-bit) | 10 , 1234567890 |
UInt8 | Unsigned integer | 8 | 0 to 255 | 10 , 200 |
UInt16 | Unsigned integer | 16 | 0 to 65535 | 10000 , 60000 |
UInt32 | Unsigned integer | 32 | 0 to 4,294,967,295 | 123456789 , 4000000000 |
UInt64 | Unsigned integer | 64 | 0 to 18,446,744,073,709,551,615 | 1000000000000 , 10000000000000 |
Float | Floating-point | 32 | ±1.40129846432481707e-45 to ±3.40282346638528860e+38 (~6 decimal digits precision) | 3.14f , -2.5f |
Double | Floating-point | 64 | ±4.94065645841246544e-324 to ±1.79769313486231570e+308 (~15 decimal digits precision) | 3.14159 , -2.71828 |
Text | ||||
Character | Single character | Variable (UTF-8 encoding) | Any valid Unicode character | 'a' , '?' , '한' |
String | Sequence of characters | Variable (UTF-8 encoding) | "Hello" , "Swift" | |
Boolean | ||||
Bool | Boolean value | 8 | true , false | |
Other | ||||
Array | Ordered collection of elements of the same type | Variable | [1, 2, 3] , ["a", "b"] | |
Dictionary | Collection of key-value pairs | Variable | ["name": "Alice", "age": 30] | |
Set | Unordered collection of unique elements | Variable | [1, 2, 3] | |
Optional | Represents a value that may be absent (nil ) | let optionalInt: Int? = nil |
- Size: The size of
String
,Array
,Dictionary
, andSet
types is dynamic and depends on the content. - Integer size: Swift provides both platform-dependent (
Int
,UInt
) and fixed-size integer types. Use the platform-dependent ones for most cases, as they are optimized for the current system. - Character size: Swift uses UTF-8 encoding for characters, so the size of a
Character
can vary depending on the character.
This table provides a good overview of Swift's basic data types. Remember that efficient memory management is handled by Swift's Automatic Reference Counting (ARC), so you usually don't need to worry about the exact size of these types in your everyday coding.
0 Comments
Post a Comment