Skip to content

Instantly share code, notes, and snippets.

View lucianoschillagi's full-sized avatar
πŸ› οΈ
Building

Luciano Schillagi lucianoschillagi

πŸ› οΈ
Building
View GitHub Profile
@lucianoschillagi
lucianoschillagi / .swift
Created December 4, 2024 12:42
Enums with mutating methods
import Foundation
enum TrafficLight {
case red, yellow, green // TrafficLight cases
mutating func next() {
switch self { // self = TrafficLight
case .red:
self = .green
case .green:
self = .yellow
@lucianoschillagi
lucianoschillagi / .swift
Created November 25, 2024 13:41
How to convert a JSON into a Swift object
import Foundation
// Converting JSON into data
let json = """
{
"name": "Dog",
"legs": 4
}
""".data(using: .utf8)!
@lucianoschillagi
lucianoschillagi / .swift
Created November 11, 2024 13:02
SwiftUI View Lifecycle methods β†’ task(priority:)
import SwiftUI
struct ContentView: View {
let url = URL(string: "https://dc4p.store")!
@State private var message = "Loading..."
var body: some View {
Text(message)
.task {
@lucianoschillagi
lucianoschillagi / .swift
Created November 10, 2024 22:00
SwiftUI View Lifecycle Methods β†’ onChange
import SwiftUI
struct ContentView: View {
// tracking changes of this property via 'onChange' method
@State private var notificationsEnabled = false
var body: some View {
Toggle("Notifications", isOn: $notificationsEnabled)
.padding()
@lucianoschillagi
lucianoschillagi / .swift
Created November 8, 2024 21:30
SwiftUI View Lifecycle Methods (onAppear, onDisappear)
import SwiftUI
struct ContentView: View {
@State private var isSheetPresented: Bool = false
var body: some View {
VStack {
Button("Show Sheet") {
isSheetPresented = true
}
@lucianoschillagi
lucianoschillagi / .swift
Created November 7, 2024 14:11
Enumerations with Associated Values
import Foundation
// Enumerations with Associated Values
enum OrderStatus {
case pending(orderID: Int, isAnOffer: Bool)
case shipped(orderID: Int, trackingNumber: String, isAnOffer: Bool)
case delivered(orderID: Int, date: String, signature: String?, isAnOffer: Bool)
}
// Usage examples
@lucianoschillagi
lucianoschillagi / .swift
Created October 30, 2024 15:52
hash values for set types
import Foundation
let beatles: Set<String> = ["John", "Paul", "George", "Ringo"] // unique values - hash value
for member in beatles {
print("'Beatle \(member)' has a hash value β†’", member.hashValue)
}
print("The Beatles was β†’", beatles.count)
@lucianoschillagi
lucianoschillagi / .swift
Last active October 21, 2024 16:01
dump function (SSL)
import Foundation
struct Beatle {
let name = "George Harrison"
let instrument = "Guitar and Vocals"
let yearOfBorn = 1943
let hisHitsSongs = ["Something", "Here comes the sun", "My Sweet Lord", "While My Guitar Gently Weeps"]
let wasAGreatMusician = true
}
@lucianoschillagi
lucianoschillagi / .swift
Created October 19, 2024 15:51
percentage graphic representation
import Foundation
enum Percentaje: String {
case ten = "β—‰ β—‹ β—‹ β—‹ β—‹ β—‹ β—‹ β—‹ β—‹ β—‹"
case twenty = "β—‰ β—‰ β—‹ β—‹ β—‹ β—‹ β—‹ β—‹ β—‹ β—‹"
case thirty = "β—‰ β—‰ β—‰ β—‹ β—‹ β—‹ β—‹ β—‹ β—‹ β—‹"
case fourty = "β—‰ β—‰ β—‰ β—‰ β—‹ β—‹ β—‹ β—‹ β—‹ β—‹"
case fifty = "β—‰ β—‰ β—‰ β—‰ β—‰ β—‹ β—‹ β—‹ β—‹ β—‹"
case sixty = "β—‰ β—‰ β—‰ β—‰ β—‰ β—‰ β—‹ β—‹ β—‹ β—‹"
case seventy = "β—‰ β—‰ β—‰ β—‰ β—‰ β—‰ β—‰ β—‹ β—‹ β—‹"
@lucianoschillagi
lucianoschillagi / creating-a-custom-environment-value.swift
Last active November 11, 2024 13:58
Creating custom environment value in SwiftUI
import SwiftUI
struct ContentView: View {
@Environment(\.myCustomValue) private var myCustomValue
var body: some View {
Text(myCustomValue).font(.title2)
}
}