Skip to content

Instantly share code, notes, and snippets.

@sisoje
sisoje / ComposableNetworking.swift
Last active July 11, 2025 06:32
The Composable Networking
import Foundation
import SwiftUI
// MARK: - errors
enum ComposableNetworkingError: Error {
case missingBearerToken
case missingRefreshToken
}
@sisoje
sisoje / Binding+Boolify.swift
Created December 13, 2023 08:15
Wrap an optional item to a bool binding
import SwiftUI
extension Binding {
static func boolify<T: Any>(_ binding: Binding<T?>) -> Binding<Bool> {
Binding<Bool> {
binding.wrappedValue != nil
} set: { newValue in
guard !newValue else {
assertionFailure()
return
import Foundation
// MARK: - typed predicate types
public protocol TypedPredicateProtocol: NSPredicate { associatedtype Root }
public final class CompoundPredicate<Root>: NSCompoundPredicate, TypedPredicateProtocol {}
public final class ComparisonPredicate<Root>: NSComparisonPredicate, TypedPredicateProtocol {}
// MARK: - compound operators
@sisoje
sisoje / UIImage+FixOrientation.swift
Last active June 25, 2023 20:32
Fix UIImage orientation
extension UIImage {
var fixOrientationTransform: CGAffineTransform {
let angle = CGFloat((imageOrientation.rawValue & 2) >> 1 - ((imageOrientation.rawValue & 1) << 1)) * .pi / 2
let flipX = CGFloat(1 - ((imageOrientation.rawValue & 4) >> 1))
return CGAffineTransform(scaleX: flipX, y: 1).rotated(by: angle)
}
}
@sisoje
sisoje / DeinitBlock.swift
Last active August 18, 2020 15:01
Resource acquisition is initialization (RAII) in Swift: Implementation
class DeinitBlock {
let onDeinit: () -> Void
init(_ block: @escaping () -> Void) {
onDeinit = block
}
deinit {
onDeinit()
}
}
import CoreData
public final class CoreDataStack {
public let persistentContainer: NSPersistentContainer
public init(_ persistentContainer: NSPersistentContainer) {
self.persistentContainer = persistentContainer
}
public private(set) lazy var mainContext: NSManagedObjectContext = {
@objcMembers
class DummyTypes: NSObject {
@NSManaged var bool: Bool
@NSManaged var decimal: Decimal
@NSManaged var int: Int
@NSManaged var double: Double
@NSManaged var url: URL
@NSManaged var uuid: UUID
@NSManaged var date: Date
@NSManaged var string: String
func inPredicateBad<S: Sequence>(_ values: S) -> NSComparisonPredicate {
let ex1 = NSExpression(forKeyPath: \S.Element.self)
let ex2 = NSExpression(forConstantValue: values)
return NSComparisonPredicate(leftExpression: ex1, rightExpression: ex2, modifier: .direct, type: .in)
}
func inPredicateOk<S: Sequence>(_ values: S) -> NSComparisonPredicate {
let ex1 = NSExpression.expressionForEvaluatedObject()
let ex2 = NSExpression(forConstantValue: values)
return NSComparisonPredicate(leftExpression: ex1, rightExpression: ex2, modifier: .direct, type: .in)
import XCTest
import Foundation
class TestFulfill: XCTestCase {
class FulfillOnRelease {
let ex: XCTestExpectation
init(_ ex: XCTestExpectation) {
self.ex = ex
}
deinit {
extension XCTestExpectation {
var fulfiller: DeinitBlock {
return DeinitBlock {
print("Fulfill \(self.description)")
self.fulfill()
}
}
}
class FillfilerTests: XCTestCase {