Skip to content

Instantly share code, notes, and snippets.

View TerryCK's full-sized avatar

Guan-Jhen (Terry) TerryCK

  • Taipei, Taiwan
View GitHub Profile
@TerryCK
TerryCK / .swift
Last active October 6, 2018 08:12
Generic class inheritance
import Foundation
protocol Network {
typealias SuccessHandler = (Array<Response>) -> Void
associatedtype Response: Codable
var payloads: Array<Response> { get }
func fire(onSuccess: SuccessHandler)
}
import Foundation
extension NSRegularExpression {
convenience init(_ pattern: String) {
do {
try self.init(pattern: pattern)
} catch {
preconditionFailure("Illeagal regular expression: \(pattern).")
}
@TerryCK
TerryCK / ImageAsset.swift
Created May 22, 2018 10:49
the enum for the image assets.
import UIKit
// case name must same as Asset Name
enum ImageAsset: CaseNameConvertible, CaseIterable {
case logoEN, logoZH
case msgTrash, msgChecked, msgUnchecked
case navbarClose, navbarBack
import UIKit
class BaseViewController {
var basicProperty: Int {
return 10
}
required init(coder: NSCoder) {
fatalError()
@TerryCK
TerryCK / GenericDataSource.swift
Created April 16, 2018 13:22
GenericDataSource for UITableView
//
// ViewController.swift
// GenericViewController
//
// Created by 陳 冠禎 on 16/04/2018.
// Copyright © 2018 AppCoda. All rights reserved.
//
import UIKit
@TerryCK
TerryCK / EnumAllcases.swift
Last active March 31, 2018 01:03
EnumAllcases.swift
protocol EnumCollection : Hashable {
static var allCases: [Self] { get }
}
extension EnumCollection {
static var allCases: [Self] {
let anySequence = AnySequence { () -> AnyIterator<Self> in
var index = 0
@TerryCK
TerryCK / enumCaseName.swift
Created March 26, 2018 12:02
enumCaseName
import UIKit
protocol CellConfigurable: RawRepresentable {
init(indexPath: IndexPath)
}
extension RawRepresentable where Self.RawValue == Int {
static var allCases: [Self] { return allCases() }
import Foundation
// Source: http://www.lintcode.com/en/problem/reverse-order-storage/
// Question: Given 1 -> 2 -> 3 -> null, return [3,2,1].
extension Collection {
func generatorLinkList(inital: inout LinkedListNode<Element>) {
reduce(into: inital) {
$0.nextNode = LinkedListNode(value: $1)
$0 = $0.nextNode!
}
}
@TerryCK
TerryCK / 822. Reverse Order Storage.swift
Created March 20, 2018 10:44
playground_Reverse_Order_Storage
import Foundation
//1 -> 2 -> 3 [3,2,1]
protocol LinkListable {
associatedtype Value
var value: Value { get }
var reference: Self? { get }
}
@TerryCK
TerryCK / enum.swift
Last active March 17, 2018 13:49
enum: Int as UITableViewCell.
import UIKit
protocol CellConfigurable: RawRepresentable {
init(indexPath: IndexPath)
}
extension RawRepresentable where Self.RawValue == Int {
static var allCase: [Self] { return all1() }
var name: String { return String(describing: self) }