Swift Types - Part Two Flashcards
Types - definition.
A construct to represent different kinds of data.
“I am a String, Because”
“I am wrapped in quotes”
How to Add Strings: (2 Ways).
Concatenation
or…
String Interpolation
String Interpolation
String Interpolation is a way to construct a new string value from a mix of other strings. Same constants as before:
let country = "United States of America" let state = "North Carolina"let city = "Charlotte"let street = "West Street"
Example: let interpolatedAddress = "\(country), \(state), \(city)"
Using String Interpolation, you can evaluate the contents of more than just strings.
Concatenation
// Concatenationlet country = "United States of America"let state = "North Carolina"let city = "Charlotte"let street = "West Street"let concatenatedAddress = country + ", " + state + ", " + city
the first method is called concatenation. This is an overly complicated sounding word, but it’s just programmer speak for adding two strings together.
Kinds of Types
- Strings
- Numbers & Booleans
An Integer (Int)
is a:
Whole #, like 42 or -25
Floating Point Numbers (Double Or Float)
Default is double
Numbers With a Fractional Component
Boolean Type (Bool)
True (1) or False (0)
Type Inference
Type inference enables the compiler to deduce the type of a particular expression automatically by examining the values we provide.
Type Safety
Cannot use an int instead of a string once declared
To Explicity State the Type of Data (examples):
// Integerlet secondValue: Int = 4// Stringlet bestPlayer: String = "Micheal Jordan"// Doublelet averagePointsPerGame: Double = 30.1// Booleanlet hallOfFame: Bool = true