Skip to content

Instantly share code, notes, and snippets.

@roymckenzie
roymckenzie / CloudKitBasics.swift
Last active August 10, 2022 12:01
Basic Subscription and Fetch manager and setup for CloudKit
import UIKit
import CloudKit
import RealmSwift
/// Handles common methods for subscriptions across multiple databases
enum CloudKitDatabaseSubscription: String {
case `private`
case `public`
}
/// - returns: `true` when dynamic type is `Equatable` and `==` returns `true`, otherwise `false`.
func areEquatablyEqual(_ lhs: Any, _ rhs: Any) -> Bool {
func receiveLHS<LHS>(_ typedLHS: LHS) -> Bool {
guard
let rhsAsLHS = rhs as? LHS
else { return false }
return areEquatablyEqual(typedLHS, rhsAsLHS)
}
return _openExistential(lhs, do: receiveLHS)
}
@andreweades
andreweades / StringIndicesPlaygroundContent.swift
Last active April 22, 2021 09:53
Ways to index String with an Int.
// How to Index String by Int
// Andrew Eades
// https://www.youtube.com/channel/UC2kOJKUGXfC01YEKH4QjFRA
// Paste this into an Xcode Playground
import Cocoa
var str = "Hello, playground"
// Simplified version of https://gist.github.com/anandabits/d9494d14fef221983ff4f1cafa318d47#file-areequatablyequal-swift
func isEqual(x: Any, y: Any) -> Bool {
func f<LHS>(_ lhs: LHS) -> Bool {
let p = Wrapper<LHS>.self as? AnyEquatable.Type
return p?.isEqual(x, y) ?? false
}
return _openExistential(x, do: f)
}