Skip to content

Instantly share code, notes, and snippets.

//
// DarwinNotificationCenter.swift
//
// Created by Nonstrict on 2023-12-07.
//
import Foundation
import Combine
private let center = CFNotificationCenterGetDarwinNotifyCenter()
/// - 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)
}
@tomlokhorst
tomlokhorst / AppDelegate.swift
Created June 6, 2019 14:55
Xcode 11 iOS 12 crash: failed to demangle witness for associated type
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print(process(MyFoo()))
return true
}
@tomlokhorst
tomlokhorst / NSUserDefaults+Keys.swift
Created July 26, 2015 12:21
Strongly typed NSUserDefaults using Phantom Types.
// We can use Phantom Types to provide strongly typed acces to NSUserDefaults
// Similar to: http://www.objc.io/blog/2014/12/29/functional-snippet-13-phantom-types/
// A key to UserDefaults has a name and phantom type
struct Key<T> {
let name: String
}
@tomlokhorst
tomlokhorst / gist:663733aa392e5dc19438
Created May 27, 2015 18:00
Json model vs Domain model
/* Domain model */
// A Profile consists of a person name and an optional avatar
struct Profile {
let personName: PersonName
let avatarURL: NSURL?
}
// These are the UX rules for showing a greeting:
@tomlokhorst
tomlokhorst / invalid-cast.swift
Created March 4, 2015 18:29
Invalid cast causing runtime crash in Swift <= 1.2
import Foundation
let obj = NSNumber(int: 2)
func correct<T>() -> T? {
if let val = obj as? T {
println("matching types")
return val
}
@tomlokhorst
tomlokhorst / Promise.java
Last active August 29, 2015 14:16
Example of wrapping a RxJava Observable to get promise semantics.
// This is now available as a full library instead of a gist: https://github.com/Q42/RxPromise
@tomlokhorst
tomlokhorst / gist:413457d8956d01638993
Created December 18, 2014 09:01
Don't zoom in on Google Maps when scrolling through a website
function fixMapScrollwheel(map) {
map.setOptions({ scrollwheel: false });
google.maps.event.addListener(map, 'click', function() {
map.setOptions({ scrollwheel: true });
});
google.maps.event.addListener(map, 'mouseout', function() {
map.setOptions({ scrollwheel: false });
});
@tomlokhorst
tomlokhorst / gist:6ed40e0e00cd2989a282
Last active August 29, 2015 14:08
SegueManager.swift
@tomlokhorst
tomlokhorst / Optional+Unwrap.swift
Last active December 26, 2017 19:50
Unwrap multiple optionals in Swift 1.0
func unwrap<T1, T2>(optional1: T1?, optional2: T2?) -> (T1, T2)? {
switch (optional1, optional2) {
case let (.Some(value1), .Some(value2)):
return (value1, value2)
default:
return nil
}
}
func unwrap<T1, T2, T3>(optional1: T1?, optional2: T2?, optional3: T3?) -> (T1, T2, T3)? {