Start learning free

LESSON 10 OF 415 · VARIABLES

Data Types in Swift: Int, Double, String, Bool and Character

Lesson Read the full explanation

TL ; DR:

In Swift, not all data is treated the same. There are different data types, such as:

  • Int - represents integer numbers like 3 and -12

  • Double - represents floating point numbers like 3.14

  • String - represents a string (sequence of characters) like "John Doe"

  • Bool - represents data that can have one of two values: true or false

  • Character - represents a single character like "a", "Z" or "?"



Full lesson:

One instruction can have several distinct meanings depending on the data types it's working with.

For example, suppose you see this line of code somewhere in a program:

print(a + b)

Can you tell for sure what it does without knowing anything about the variables a and b?

It might add two numbers, if a and b are both numbers:

var a = 10
var b = -5

print(a + b) // Output: 5

But it also might concatenate two strings, if a and b are both strings:

var a = "Hello "
var b = "world!"

print(a + b) // Output: Hello world!

Of course, addition and string concatenation are two completely different operations and Swift needs to figure out which one to do.

And this is exactly why Swift assesses the data type of every value you use in your program. In order to be able to make these decisions when needed.

In our example, when it reads a + b, Swift checks the data type of a and b. If both are strings, it performs string concatenation. If both are numbers, it performs addition.


Invalid operations:

Assessing data types is also helpful for Swift to make sure that what you do with the data actually makes sense.

For example, computers can perform mathematical operations on numbers, but not on strings. Of course, nobody can stop you from writing a program like this:

var a = "peanut"
var b = "butter"

print(a * b) // error: binary operator '*' cannot be applied to two 'String' operands

But when you try to run this program, the compiler will report an error: binary operator '*' cannot be applied to two 'String' operands.

This is the way of Swift telling you: "Hey, I'm a little confused. How do you multiply two strings? I've never heard of that."


Assignment
Follow the Coding Tutorial and let's practice with data types!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 4
    Ctrl + Enter to check

    SWIFT · VARIABLES

    Data Types in Swift, explained

    What a data type is

    Every value in a Swift program has a type, which says what kind of data it is and what you can do with it. The five types you meet first are:

    TypeHoldsExample
    Intwhole numbers3, -12
    Doublenumbers with a fractional part3.14
    Stringtext, a sequence of characters"John Doe"
    Booltrue or falsetrue
    Characterone single character"a", "?"

    A Character is written with double quotes like a string. Swift treats a literal as a String unless you ask for a Character with an annotation: let grade: Character = "A".

    Why types matter

    The same symbol can mean different things for different types. In print(a + b), the + adds when a and b are numbers and joins text when they are strings:

    var a = 10
    var b = -5
    print(a + b)
    
    var c = "Hello "
    var d = "world!"
    print(c + d)
    
    5
    Hello world!
    

    Swift checks the types while it compiles the code. When an operation makes no sense for a type, it refuses to build the program. Two strings cannot be multiplied:

    var a = "peanut"
    var b = "butter"
    print(a * b)
    
    error: binary operator '*' cannot be applied to two 'String' operands
    

    The error shows up before anything runs, which is why Swift is described as type-safe.

    Declaring a type

    You can state a type explicitly with an annotation, a colon and the type name after the constant's name:

    let score: Int = 5
    

    You can also leave it out and let Swift infer the type from the value. A whole-number literal becomes an Int, a literal with a decimal point becomes a Double, a quoted literal becomes a String and true or false becomes a Bool. An annotation can override the inference: let ratio: Double = 7 stores 7.0.

    Where each type shows up

    A Bool is usually the result of a comparison, and later it decides which code runs. With let age = 20, the line let isAdult = age >= 18 gives isAdult the value true and the type Bool.

    A Double stores approximations of decimal numbers, so small rounding differences appear: 0.1 + 0.2 prints 0.30000000000000004. An Int is exact, which makes it the right choice for counts and ages.

    A String knows how many characters it holds: "swift".count is 5. A number has no count.

    The exercise, step by step

    The lesson asks for four constants, one per type, each on its own line and in this order. Each step keeps the earlier ones.

    1. myInt with type Int, set to a whole number.
    2. myFloat with type Double, set to a value with a decimal point. The name says float, but the type to use in Swift is Double.
    3. myString with type String, set to some text in double quotes.
    4. myBool with type Bool, set to true or false.

    The solution in Swift

    let myInt: Int = 5
    let myFloat: Double = 3.14
    let myString: String = "Hello"
    let myBool: Bool = true
    

    The details a learner should know:

    • Int and Double never mix silently. With let a = 10 and let b = 2.5, the expression a * b is a compile error, because one operand is an Int and the other a Double. Convert one side: Double(a) * b is 25.0.
    • Integer division truncates. 7 / 2 is 3, while 7.0 / 2 is 3.5.
    • Int is 64 bits on 64-bit platforms, so it runs from -9223372036854775808 to 9223372036854775807.
    • The type is fixed. A var holding an Int cannot later hold a string.
    • Use type(of:) to check. type(of: 3.14) prints Double.

    Common mistakes

    • Quotes around a number. let myInt: Int = "5" does not compile, as the strings-versus-numbers lesson shows.
    • Capital letters in true and false. Swift's booleans are lowercase. True is an unknown name.
    • Single quotes for text. Swift strings and characters use double quotes.

    Key takeaways

    • The core types are Int, Double, String, Bool and Character.
    • Swift checks types at compile time and rejects operations that do not fit.
    • Annotate with name: Type, or let Swift infer the type from the value.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free