Skip to content

Instantly share code, notes, and snippets.

@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)
@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)
struct Filter<Object> {
init<Type>(keyPath: KeyPath<Object, Type>, matcher: @escaping (Type) -> Bool) {
}
}
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
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
@vhart
vhart / Filter.swift
Created June 7, 2018 18:49
Writing Filters Using KeyPaths
struct Filter<Object> {
let path: PartialKeyPath<Object>
let matcher: (Any) -> Bool
init<Type>(keyPath: KeyPath<Object, Type>, matcher: @escaping (Type) -> Bool) {
self.path = keyPath
self.matcher = { value in
guard let typedValue = value as? Type else { return false }
return matcher(typedValue)
import UIKit
class PermaColorView: UIView {
var color: UIColor = .clear {
didSet {
backgroundColor = color
}
}
override var backgroundColor: UIColor? {
@vhart
vhart / Self-Sizing-TableView-Cells-pt1.swift
Last active April 11, 2018 22:40
Self sizing chat cells
import UIKit
class ChatCell: UITableViewCell {
lazy var iconImageView: UIImageView = {
let imageView = UIImageView()
let icon = UIImage(named: "icons8-Sheep on Bike")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.backgroundColor = .clear
imageView.layer.cornerRadius = 25
imageView.contentMode = .scaleAspectFit
@vhart
vhart / Swift4-ReadWriteDelete.swift
Last active November 8, 2022 16:15
File Handling from an Xcode Playground
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
func setUpDemo() {
let testContents = """
This is a test
Emojis follow:
🚀
@vhart
vhart / MultiTouchViewAction.kt
Created January 12, 2018 17:23
Multi Touch View Action For Espresso
import android.support.test.espresso.ViewAction
import android.support.test.espresso.ViewInteraction
import android.support.test.espresso.action.CoordinatesProvider
import android.support.test.espresso.UiController
import android.view.InputDevice
import android.view.MotionEvent
import android.os.SystemClock
class MultiTouchDownEvent : ViewAction {