This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Minimalist implementation of ReversedCollectionProtocol */ | |
public protocol ReversedCollectionProtocol: BidirectionalCollection { | |
associatedtype Base: BidirectionalCollection | |
var _base: Base { get } | |
} | |
extension ReversedCollection: ReversedCollectionProtocol {} | |
extension LazyCollectionProtocol |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol P1 { | |
init() | |
func bar() | |
} | |
extension P1 { | |
init() { | |
self.init() | |
print("P1 init") | |
} | |
func bar() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])! | |
} |