Skip to content

Instantly share code, notes, and snippets.

View Moximillian's full-sized avatar

Mox Soini Moximillian

View GitHub Profile
@Moximillian
Moximillian / Self or associatedtype.swift
Last active November 26, 2018 21:04
Journey into PAT-land
/**
* A journey in trying to provide unified interface to UITableView and UICollection
*/
protocol UnifiedCell { /* specific features omitted, as they are not relevant for this journey */ }
extension UITableViewCell: UnifiedCell {}
extension UICollectionViewCell: UnifiedCell {}
// the idea of the deque() below is that a unified code can be built
// that uses single codepath to call collection.deque() on both UITableViews and UICollections
/* Minimalist implementation of ReversedCollectionProtocol */
public protocol ReversedCollectionProtocol: BidirectionalCollection {
associatedtype Base: BidirectionalCollection
var _base: Base { get }
}
extension ReversedCollection: ReversedCollectionProtocol {}
extension LazyCollectionProtocol
import Foundation
extension NSObjectProtocol where Self: NSObject {
internal func store(associatedObject: Any) {
// store the container so that it can be called later, we do not need to explicitly retrieve it.
var associatedObjectStore = objc_getAssociatedObject(self, Unmanaged.passUnretained(self).toOpaque()) as? [Any] ?? []
associatedObjectStore.append(associatedObject)
objc_setAssociatedObject(self, Unmanaged.passUnretained(self).toOpaque(), associatedObjectStore, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
@Moximillian
Moximillian / InitInProtocol.swift
Last active April 28, 2018 12:03
Example of how init/func behaves in protocols and protocol extensions
protocol P1 {
init()
func bar()
}
extension P1 {
init() {
self.init()
print("P1 init")
}
func bar() {
@Moximillian
Moximillian / ClosureExamples.swift
Created April 22, 2018 16:35
Examples how to use closures
/* regular closure */
/// definition of function with closure parameter
func test(closure: (Int) -> String) {
let result = closure(42)
print("Result is \(result)")
}
// using the function
test { i in
@Moximillian
Moximillian / .block
Created March 22, 2017 15:26
bar chart
license: mit
@Moximillian
Moximillian / .block
Last active March 23, 2017 10:01
bar chart
license: mit
@Moximillian
Moximillian / .block
Last active March 22, 2017 14:12
fresh block
license: mit
@Moximillian
Moximillian / ClosureProtocolExtensions.swift
Last active February 17, 2021 03:47
Swift protocol and extensions for adding support to closures in UIKit classes. Extension support for UIButton, UIGestureRecognizer, UIBarButtonItem etc. Swift 3.0.
// Free to copy, use and modify for all types of software projects
import UIKit
// ************** Protocol ***************
/// Closurable protocol
protocol Closurable: class {}
// restrict protocol to only classes => can refer to the class instance in the protocol extension
extension Closurable {
@Moximillian
Moximillian / StringRangeRawValue.swift
Last active March 30, 2016 18:21
New valuetype for enums to convert a minimum and maximum value into Range<Int>
import Foundation
/// StringRangeRawValue – new valuetype that is compatible with RawValue => can be used as enum valuetype
struct StringRangeRawValue: StringLiteralConvertible, Equatable {
let range: Range<Int>
static func convert(value: String) -> Range<Int> {
let values = value.componentsSeparatedByString(",")
return Int(values[0])! ..< Int(values[1])!
}