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 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)
}
}
@lucianoschillagi
lucianoschillagi / .swift
Last active October 12, 2024 00:30
json decoder
import Foundation
// Converting JSON into data
let json = """
{
"name": "Dog",
"legs": 4
}
""".data(using: .utf8)!
@lucianoschillagi
lucianoschillagi / .swift
Created October 9, 2024 13:00
converting an function into a computed property
import Foundation
struct Cat {
// func saySomething() -> String {
// return "Muuu! I mean 'Miau!'"
// }
// Now, I am going to convert this method into a computed property
var saySomething: String {
return "Muuu! I mean 'Miau!'"