Skip to content

Instantly share code, notes, and snippets.

View codelynx's full-sized avatar

Kaz Yoshikawa codelynx

View GitHub Profile
@codelynx
codelynx / ZWeakObjectSet.swift
Last active October 26, 2020 01:54
A Swift class that manages weak objects
// WeakObjectSet.swift
//
// The MIT License (MIT)
//
// Copyright (c) 2020 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@codelynx
codelynx / ZSearchField.swift
Created March 23, 2016 19:44
ZSearchField is able to detect when focused or unfocused to its delegate. If you are using Interface Builder make sure to change the NSSerachField class to ZSearchField, and t's delegate must conforms to ZSearchFieldDelegate not NSTextFieldDelegate.
protocol ZSearchFieldDelegate: NSTextFieldDelegate {
func searchFieldDidBecomeFirstResponder(textField: ZSearchField)
func searchFieldDidResignFirstResponder(textField: ZSearchField)
}
class ZSearchField: NSSearchField, NSTextDelegate {
var expectingCurrentEditor: Bool = false
@codelynx
codelynx / Array+addition.swift
Last active March 24, 2016 13:14
Extension for Array to remove an object from it.
extension Array where Element: Equatable {
mutating func remove<Element: Equatable>(object: Element) -> Array {
self = self.filter { $0 as? Element != object }
return self
}
}
@codelynx
codelynx / ZMapTable.swift
Created March 26, 2016 07:12
NSMapTable wrapper
//
// ZMapTable.swift
// ZKit
//
// Created by Kaz Yoshikawa on 3/26/16.
// Copyright © 2016 Electricwoods LLC. All rights reserved.
//
import Foundation
@codelynx
codelynx / ZMapTable.swift
Last active September 26, 2018 01:20
Swifty NSMapTable that can deal with weak referenced object - subclassing edition
// ZMapTable.swift
//
// Copyright (c) 2016 Kaz Yoshikawa. Released under MIT License.
class ZMapTable<K: AnyObject, V: AnyObject>: CustomStringConvertible {
let mapTable: NSMapTable
private init(mapTable: NSMapTable) {
self.mapTable = mapTable
}
@codelynx
codelynx / ZMapTable.swift
Last active August 10, 2021 08:11
Swifty NSMapTable that can deal with weak referenced objects - protocol extension edition
// ZMapTable.swift
//
// Copyright (c) 2016 Kaz Yoshikawa. Released under MIT License.
protocol ZMapTableProtocol: CustomStringConvertible {
associatedtype K: AnyObject
associatedtype V: AnyObject
var mapTable: NSMapTable { get }
// ZMapTable.swift
//
// Copyright (c) 2016 Kaz Yoshikawa. Released under MIT License.
enum ZMapTableOption {
case StrongToWeak
case WeakToStrong
case WeakToWeak
case StrongToStrong
}
@codelynx
codelynx / UIView+Find.swift
Created March 30, 2016 16:02
UIView extension to find a specific UIViewController type in responder chain. And UIView extension to find specific UIView subclass in view hierarchy toward superview.
// UIView+Find.swift
//
// Copyright (c) 2016 Kaz Yoshikawa. Released under MIT License.
extension UIView {
func findViewController<T: UIViewController>() -> T? {
var responder = self.nextResponder()
while responder != nil {
if let viewController = responder as? T {
@codelynx
codelynx / ZCache.swift
Last active February 3, 2018 06:22
Make NSCache more generics with Swift.
import Foundation
class ZCache<T: AnyObject> {
private var cache = NSCache()
var delegate: NSCacheDelegate? {
get { return self.delegate }
set { self.cache.delegate = newValue }
@codelynx
codelynx / ZCollection.swift
Created May 16, 2016 18:33
Class version of: Array, Set, Dictionary
//
// ZCollection.swift
// ZKit
//
// Created by Kaz Yoshikawa on 2015/01/12.
// Copyright (c) 2015 Kaz Yoshikawa. All rights reserved.
//
// This software may be modified and distributed under the terms
// of the MIT license.
//