Skip to content

Instantly share code, notes, and snippets.

View andresr-dev's full-sized avatar

Andres Raigoza andresr-dev

View GitHub Profile
@andresr-dev
andresr-dev / CodableClass.swift
Created March 31, 2023 19:45
This is how to conform to the Codable protocol within a class in swift
import Foundation
final class User: ObservableObject, Codable {
enum CodingKeys: CodingKey {
case name
}
@Published var name = "Paul Hudson"
required init(from decoder: Decoder) throws {
@andresr-dev
andresr-dev / ForceDarkMode.swift
Created March 25, 2023 19:23
This is how to force dark mode in your application when the SwiftUI .preferedColorScheme() is not enough
let scenes = UIApplication.shared.connectedScenes
guard let scene = scenes.first as? UIWindowScene else { return }
scene.keyWindow?.overrideUserInterfaceStyle = .dark
@andresr-dev
andresr-dev / LayoutWarningDisable.swift
Created March 16, 2023 16:44
This is how to disable the layout warnings in swift
UserDefaults.standard.set(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable")
@andresr-dev
andresr-dev / Array-Sorted.swift
Created March 13, 2023 16:23
This is how to check if an array is sorted
extension Array where Element: Comparable {
func isSorted() -> Bool {
self == self.sorted()
}
}
@andresr-dev
andresr-dev / ClockDuration.swift
Last active March 13, 2023 14:51
This is how to measure time execution in swift
let clock = ContinuousClock()
let start = clock.now
// Do some heavy calculation...
print("Took \(start.duration(to: .now))")
@andresr-dev
andresr-dev / AuthenticationWithBiometrics.swift
Last active February 20, 2023 21:47
This is how to authenticate a user with biometrics in swift
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?
@andresr-dev
andresr-dev / CoreDataFileProtection.swift
Last active February 20, 2023 20:14
This is how to setup core data with in memory option and complete file protection
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")
@andresr-dev
andresr-dev / CoreDataDeleteBatch.swift
Created February 19, 2023 23:03
This is how to perform a batch delete in CoreData
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.
@andresr-dev
andresr-dev / OptionalBindingOperator.swift
Created February 7, 2023 17:03
This is a custom operator that allows you to perform nil coalescing with optional bindings in swift
import SwiftUI
public func ??<T>(lhs: Binding<Optional<T>>, rhs: T) -> Binding<T> {
Binding(
get: { lhs.wrappedValue ?? rhs },
set: { lhs.wrappedValue = $0 }
)
}
// MARK: - USE
@andresr-dev
andresr-dev / AutomaticGrammarAgreement.swift
Created January 30, 2023 00:34
This is how to apply automatic grammar agreement on a localizable file
// 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";