This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 == "😎", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| @main | |
| struct MyApp: App { | |
| @Environment(\.scenePhase) var scenePhase | |
| var body: some Scene { | |
| WindowGroup { | |
| ContentView() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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. | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct ContentView: View { | |
| var body: some View { | |
| Link("Go to 'Link' documentation", | |
| destination: URL(string: "https://developer.apple.com/documentation/swiftui/link")!) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct ContentView: View { | |
| let myView = "I am a draggable view!" | |
| var body: some View { | |
| Text(myView) | |
| .font(.title) | |
| .draggable(myView) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!'" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| // Converting JSON into data | |
| let json = """ | |
| { | |
| "name": "Dog", | |
| "legs": 4 | |
| } | |
| """.data(using: .utf8)! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct ContentView: View { | |
| @Environment(\.myCustomValue) private var myCustomValue | |
| var body: some View { | |
| Text(myCustomValue).font(.title2) | |
| } | |
| } |