Swift, like all programming languages, designate certain data types that help the operating system and computer hardware allocate memory based on what is going to be stored. These data types include numeric, textual, and logical values.
In a type-safe language like Swift, values are generally stored in variables, which are containers that hold data. The data type determines how big that variable container is and where the computer is going to store it for later access.
Basic Data Types
||Integer whole number|
||Floating point number|
||Floating point number|
Integer Data Types
Integers are whole numbers such as
-123. As seen from the example column above, they can be either signed or unsigned whole numbers, the default being signed. Integers can be declared in five different ways:
|Type Reference||Description||Value Range|
||The standard reference used for whole numbers in Swift.||Based on the platform (
||Creates an 8-bit signed integer.||-128 &endash; 127|
||Creates a 32-bit signed integer.||-2,147,483,648 &endash; 2,147,483,647|
||Creates a 64-bit signed integer.||-9,223,372,036,854,775,808 &endash; 9,223,372,036,854,775,807|
||Unsigned that is created in the same manner as a standard
||Positive values only.|
||Creates an 8-bit unsigned integer.||0 &endash; 255|
||Creates a 32-bit unsigned integer.||0 &endash; 4,294,967,295|
||Creates a 64-bit unsigned integer||0 &endash; 18,446,744,073,709,551,615|
let shinyNewInteger: Int = 500let verySmallInteger: Int8 = 16
Floating Point Numbers
Float and a
Double are number data types that allow for decimals. A
Float is a 32-bit ‘floating-point’ number and a
Double is a 64-bit floating-point number, that being said, a
Float has approximately half as much precision as a
Double. If a high precision of accuracy is needed, it is best to use a
Double. When a variable is declared without a specified type, Swift will type inference a
Double as a precaution.
let accountBalance: Float = 857.45let pi: Double = 3.14159265359let gpa = 3.7 // inferred as a Double
Strings and Characters
Strings are a collection of
Characters are the individual symbols that make up our languages. In Swift, the
String type can be either mutable or immutable, as determined by the type of variable it’s stored in, either a
var or a
let for a constant. Both
Character are typically declared inside a set of double quotation marks, while multi-line
Strings are declared with a set of triple quotations opening and closing the text.
let author: String = "Edgar Allen Poe"let type: Character = "P"let theRaven: String = """Once upon a midnight dreary, while I pondered, weak and weary,Over many a quaint and curious volume of forgotten lore—While I nodded, nearly napping, suddenly there came a tapping,As of some one gently rapping, rapping at my chamber door.“’Tis some visitor,” I muttered, “tapping at my chamber door—Only this and nothing more.”"""
Boolean Data Type
Boolean values, initialized using the
Bool keyword, represent
false. They are used in control flow and other conditional statements to process the logical decision points in the program leading them to be referred to as logical values. They can be declared directly or by using a logical test.
let fallingOnPavementHurts = truevar gameOver: Bool = homeScore > 5 || awayScore > 5
- Anonymous contributorAnonymous contributor3077 total contributions
- christian.dinh2481 total contributions
- anli210 total contributions