Skip to content

Instantly share code, notes, and snippets.

enum FilterValue {
case string(String)
case bool(Bool)
func matches(value: Any) -> Bool {
switch self {
case .string(let stringValue):
return (value as? String) == stringValue
case .bool(let boolValue):
return (value as? Bool) == boolValue
struct MyStruct {
let name: String
let number: Int
var bool: Bool
}
let instance = MyStruct(name: "Korg", number: 1, bool: true)
// Referencing
let nameRef: KeyPath<MyStruct, String> = \MyStruct.name
struct Filter<Object> {
init<Type>(keyPath: KeyPath<Object, Type>, matcher: @escaping (Type) -> Bool) {
}
}
@vhart
vhart / Filter2.swift
Last active June 9, 2018 21:41
Filter2
struct Filter<Object> {
let keyPath: PartialKeyPath<Object>
let matcher: (Any) -> Bool
init<Type>(keyPath: KeyPath<Object, Type>, matcher: @escaping (Type) -> Bool) {
self.keyPath = keyPath
self.matcher = { value in
guard let typedValue = value as? Type else { return false }
return matcher(typedValue)
@vhart
vhart / Filter3.swift
Last active June 9, 2018 21:41
Filter3
struct Filter<Object> {
let keyPath: PartialKeyPath<Object>
private let matcher: (Any) -> Bool
init<Type>(keyPath: KeyPath<Object, Type>, matcher: @escaping (Type) -> Bool) {
self.keyPath = keyPath
self.matcher = { value in
let typedValue = value as! Type
return matcher(typedValue)