Skip to content

Instantly share code, notes, and snippets.

View lucianoschillagi's full-sized avatar

Luciano Schillagi lucianoschillagi

View GitHub Profile
@lucianoschillagi
lucianoschillagi / .swift
Created September 12, 2024 22:01
precondition method
import Foundation
// Swift Standard Library - Exploring its methods!
// The 'precondition' method.
let faceWithSunglasses: Character = "🙂"
//let faceWithSunglasses: Character = "😎"
// if this precondition is not true, the program will be interrupted
precondition(faceWithSunglasses == "😎",
@lucianoschillagi
lucianoschillagi / .swift
Created September 26, 2024 14:57
SwiftUI Scene Phases
import SwiftUI
@main
struct MyApp: App {
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
@lucianoschillagi
lucianoschillagi / .swift
Last active September 29, 2024 15:39
Application will terminate method (SwiftUI integration)
/*
If you need more control over app termination, you can integrate UIApplicationDelegate methods within SwiftUI.
In particular, applicationWillTerminate gets called right before the app is terminated,
but it won’t be called if the user force quits the app.
To use this in SwiftUI:
Create an AppDelegate class that conforms to UIApplicationDelegate.
Use UIApplicationDelegateAdaptor to link it to your SwiftUI app.
*/
@lucianoschillagi
lucianoschillagi / .swift
Created September 30, 2024 13:31
Link (SwiftUI Control=
import SwiftUI
struct ContentView: View {
var body: some View {
Link("Go to 'Link' documentation",
destination: URL(string: "https://developer.apple.com/documentation/swiftui/link")!)
}
}
@lucianoschillagi
lucianoschillagi / .swift
Created October 2, 2024 13:37
environment values (color scheme example)
import SwiftUI
struct ContentView: View {
// @Environment: A property wrapper that reads a value from a view’s environment.
// Access the 'colorScheme' from the environment
// The current 'colorScheme' used by the system.
@Environment(\.colorScheme) var colorScheme
@lucianoschillagi
lucianoschillagi / .swift
Created October 3, 2024 16:05
draggable view
import SwiftUI
struct ContentView: View {
let myView = "I am a draggable view!"
var body: some View {
Text(myView)
.font(.title)
.draggable(myView)
}
@lucianoschillagi
lucianoschillagi / .swift
Created October 8, 2024 14:23
swiftui environment values → calendar (use case)
import SwiftUI
struct CalendarView: View {
@Environment(\.calendar) var calendar // Access the user's calendar environment value
// Computed property to get the current date in the user's calendar
var currentDateInUserCalendar: String {
let formatter = DateFormatter()
formatter.calendar = calendar // Use the user's calendar
@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!'"
@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 / 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)
}
}