- Mar 18, 2025
Day 1 of 100 Days of SwiftUI: Variables, constants, strings, and numbers
So today I've really started with the SwiftUI course and by writing some code focused on variables, constants, strings and numbers. I went a bit further by mistake, but I'm sharing all what I did today here anyways.
Reflections of the day
Outside of the code I've wrote I quickly learned a few things about Xcode:
It's pretty nice how it autocompletes stuff like () or adds the error messages
The predictive code suggestion is really good, but very disturbing when you're trying to learn a new coding language. It basically gives you what you are slowly trying to write in one go. So I disable it to really learn that new language
Running the code is slow, I don't know if it's because Xcode hasn't download everything yet, but running the code is slow and some parts just don't run for now like the uppercased() thing
Code of the day
Here is the code I've written today as part of the course:
Code written to learn
import UIKit
var greeting = "Hello, playground"
// Variables
var name = "Daniele"
name = "Philippe"
name = "Joelle"
// Constant
let lastName = "Catalanotto"
// Printing stuff
print (lastName)
// Strings the content within the ""
let actor = "Denzel Washington"
// Multi-line strings start with three "
let poem = """
Life is like a box of chocolates
I hope I find one that is good
"""
// Counting what's in a string
print (poem.count)
let numberOfCharacters = poem.count
print (numberOfCharacters)
// Making a string uppercase, don't forget the ()
print (poem.uppercased())
// Has prefix or suffix
print (poem.hasSuffix("good"))
print (poem.hasPrefix("Life"))
// Working with whole numbers
let score = 10
// Breaking big numbers for better readability
let bigNumber = 1_000_000
var finalScore = score - 10
print (finalScore)
// Quick maths
var counter = 10
counter = counter + 1
// Even quicker maths: add directly something to a variable without having to say the name of the variable again. These are called compound assignment operators
counter += 1
counter -= 1 // to do minus
counter *= 2 // to multipliy
// Checking numbers, for example verifying if a number is a multiple of something
print(counter.isMultiple(of: 3))
// Working with numbers with decimals (you can't mix integers and Double (numbers with decimals)) you have to change their type
let a = 1 // Int
let b = 2.2 // Double
let c = a + Int(b)
// Booleans: true or false
var danieleIsNice = true
var hitlerIsNice = false
// Switching the value of Booleans with the ! or with .toggle() which is shorter
danieleIsNice = !danieleIsNice
print(danieleIsNice)
danieleIsNice.toggle()
print(danieleIsNice)
// Joining strings together
let bookSeries = "Service Design Principles"
let bookTitle = "401-500"
var bookOfTheMoment = bookSeries + " " + bookTitle
// String Interpolation, adding stuff directly within a string
bookOfTheMoment = "At the moment I'm writing the book \(bookSeries) \(bookTitle) in the series called \(bookSeries)"
Tiny project: Celsius to Farenheit
import UIKit
// Goal: convert temperatures from Celsius to Fahrenheit.
// Creates a constant holding any temperature in Celsius.
let roomTemperature = 20.0
// Converts it to Fahrenheit by multiplying by 9, dividing by 5, then adding 32.
let temperatureForAmericans = ((roomTemperature * 9 ) / 5 ) + 32
// Prints the result for the user, showing both the Celsius and Fahrenheit values.
print ("The room temperature is \(roomTemperature) degrees in Celsius and \(temperatureForAmericans) for Americans")