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 / enums.swift
Created February 27, 2018 12:42
class inheritance enum
import Foundation
class ClassA {
private lazy var cellType = CellType.footer
init(_ x: Int) {
self.cellType = .footer
print("ClassA", self.cellType)
type(of: self.cellType)
}
@TerryCK
TerryCK / Swift Functors, Applicatives, and Monads in Pictures.swift
Created January 6, 2018 02:45
Swift Functors, Applicatives, and Monads in Pictures
//: This is a companion Playground for the article "Swift Functors, Applicatives, and Monads in Pictures", available at http://www.mokacoding.com/blog/functor-applicative-monads-in-pictures/
//: The article itself is a translation of the original "Functors, Applicatives, and Monads in Pictures" written for Haskell by Aditya Bhargava, available at http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
//:
//: ## Optional
//:
//: Optional is just a type
enum MyOptional<T> {
case Some(T)
case None
//
// Language.swift
//
// Created by Terry Chen on 2017/12/19.
//
import UIKit
@TerryCK
TerryCK / BatteryStatus.swift
Last active December 27, 2017 06:00
iOSTaipei
import Foundation
protocol BatteryStatusProtocol {
static var allStatus: [BatteryStatus] { get }
mutating func setStatus<T: BinaryInteger>(input: T)
}
enum BatteryStatus: String, BatteryStatusProtocol {
case full, medium, less, charging, notOffcial
}
// Implement
@TerryCK
TerryCK / String+Extension.swift
Created December 26, 2017 15:03
String+Extension in playground
extension String {
static var txtLocoal: String {
return "txtLocoal".localized()
}
func localized() -> String {
return "localized String \(self)"
}
}
@TerryCK
TerryCK / strategy.swift
Last active December 26, 2017 10:41
strategy runtime error
protocol Presentable {
func presenting(with viewController: UIViewController)
}
extension Presentable where Self: UIViewController {
func presenting(with viewController: UIViewController) {
switch self {
case let pushableViewContorller as Pushable:
@TerryCK
TerryCK / ChristmasTree.swift
Last active December 25, 2017 01:57
ChristmasTree.swift
extension BinaryInteger {
func printPyramid() {
guard self > 0 else { return }
for i in 0...up {
// let offset = i <= 9 ? "0" : ""
let spaceStr = Array(repeating: " ", count: up - i).joined()
let numsStr = Array(0...i).toString
// let numsStr = Array(repeating: "\(offset)\(i.specStringInternal)", count: i).joined()
print(spaceStr + numsStr)
@TerryCK
TerryCK / threadSafety.swift
Created December 11, 2017 15:41
Discuss about Swift threadSafety with struct and memory address
import Foundation
var array = [1,2,3,4]
// read the `array` to property `getArray`, to properties in the same heap momery address, not copy data form `array` to `getArray` right now.
let getArray = array
array
getArray
address(of: array)
import Foundation
enum Status: Int {
case modified, create
}
struct Model {
var property: Int {
didSet{ self.status = .modified }
}
var modelName: String {
@TerryCK
TerryCK / iBook.swift
Created November 12, 2017 06:53
iBook.swift
class Person {
var age: Int
var residence: Residence?
init(age: Int, resdience: Residence?) {
self.age = age
self.residence = resdience
}
}
class Residence { var address: Address? }