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 | |
final class User: ObservableObject, Codable { | |
enum CodingKeys: CodingKey { | |
case name | |
} | |
@Published var name = "Paul Hudson" | |
required init(from decoder: Decoder) throws { |
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
let scenes = UIApplication.shared.connectedScenes | |
guard let scene = scenes.first as? UIWindowScene else { return } | |
scene.keyWindow?.overrideUserInterfaceStyle = .dark |
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
UserDefaults.standard.set(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable") |
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
extension Array where Element: Comparable { | |
func isSorted() -> Bool { | |
self == self.sorted() | |
} | |
} |
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
let clock = ContinuousClock() | |
let start = clock.now | |
// Do some heavy calculation... | |
print("Took \(start.duration(to: .now))") |
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 LocalAuthentication | |
class AuthenticationService { | |
// We need to provide a FaceID description in info.plist: | |
// Privacy - Face ID Usage Description -> Identify yourself! | |
func authenticate() { | |
let context = LAContext() | |
var error: NSError? | |
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 CoreData | |
class DataController { | |
let container: NSPersistentContainer | |
/// Core Data initialization | |
/// - Parameter inMemory: For testing and previewing purposes, we create a temporary, | |
/// in-memory database by writing to /dev/null so our data is destroyed after our data finishes running. | |
init(inMemory: Bool = false) { | |
container = NSPersistentContainer(name: "Main") |
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
private func delete(_ fetchRequest: NSFetchRequest<NSFetchRequestResult>) { | |
let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) | |
// Tell me the ids of the objects that you are going to delete | |
batchDeleteRequest.resultType = .resultTypeObjectIDs | |
// execute the deletion at the SQL level | |
if let deleteResult = try? container.viewContext.execute(batchDeleteRequest) as? NSBatchDeleteResult { | |
// get the IDs of the objects deleted | |
if let objectIDs = deleteResult.result as? [NSManagedObjectID] { | |
// Merge the deletions into the app's managed object context. |
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 | |
public func ??<T>(lhs: Binding<Optional<T>>, rhs: T) -> Binding<T> { | |
Binding( | |
get: { lhs.wrappedValue ?? rhs }, | |
set: { lhs.wrappedValue = $0 } | |
) | |
} | |
// MARK: - USE |
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
// Localizable.string | |
"small" = "small"; | |
"medium" = "medium"; | |
"espresso" = "espresso"; | |
"cappuccino" = "cappuccino"; | |
// 1 small cappuccino - $2.99 | |
"^[%lld %@ %@](inflect: true) - **$%@**" = "%1$lld %2$@ %3$@ - **$%%4$@**"; | |
// Localizable.string - Spanish | |
"small" = "chico"; |