Skip to content

Instantly share code, notes, and snippets.

View zyuanming's full-sized avatar
🎯
Focusing

YuanMing zyuanming

🎯
Focusing
View GitHub Profile
// Before
let start = 10
var i = start
while i < 5 {
// do something
i += 1
}
// After
@zyuanming
zyuanming / Extension
Last active June 28, 2017 02:05
ReactiveCocoa & ReactiveSwift
extension Signal {
func observeUI() -> Signal<Value, Error> {
return observe(on: UIScheduler())
}
}
extension SignalProducer {
func observeUI() -> SignalProducer<Value, Error> {
return lift { $0.observeUI() }
}
@zyuanming
zyuanming / UICollectionViewFlowLayout.swift
Last active June 15, 2017 02:28
UICollectionViewFlowLayout
class TVPodcastFlowLayout: UICollectionViewFlowLayout {
override func prepare() {
super.prepare()
register(TVCollectionReusableView.self, forDecorationViewOfKind: TVCollectionReusableView.identifier)
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var allAttributes = attributes
@zyuanming
zyuanming / UITableView+Dequeue.swift
Last active June 15, 2017 02:27
UITableView+Dequeue
import UIKit
extension UITableView
{
// MARK: - Registering Cell Types
/// Yields the built-in reuse identifier for the specified cell type.
///
/// - parameter cellType: The cell type.
private func reuseIdentifier<T: UITableViewCell>(cellType: T.Type) -> String
{
// http://swift.gg/2015/10/27/swift-pattern-matching-in-detail/
// 遍历目录
guard let enumerator = NSFileManager.defaultManager().enumeratorAtPath("/customers/2014/")
else { return }
for url in enumerator {
switch (url.pathComponents, url.pathExtension) {
@zyuanming
zyuanming / Indexable
Last active April 19, 2017 04:44
Swift
import Foundation
extension Indexable {
subscript(safeIndex safeIndex: Index) -> _Element? {
return safeIndex.distance(to: endIndex) > 0 ? self[safeIndex] : nil
}
}
@zyuanming
zyuanming / reduce
Created April 19, 2017 10:12
Swift
struct Stock {
var name = ""
var id = ""
}
let stock1 = Stock(name: "name", id: "1")
let stock2 = Stock(name: "name", id: "2")
let stock3 = Stock(name: "name", id: "3")
let stock4 = Stock(name: "name", id: "4")
let stock5 = Stock(name: "name", id: "5")
@zyuanming
zyuanming / Timer
Last active June 15, 2017 02:26
Timer not retain view controller
// http://www.futantan.com/2016/04/14/NSTimer-tips/#more
extension NSTimer {
private class FTTimerClosureWraper {
private (set) var timerClosure: () -> ()
init(timerClosure: () -> () ) {
self.timerClosure = timerClosure
}
}
@zyuanming
zyuanming / Json Parser
Last active May 16, 2017 03:24
Applicative Parsing with Documentation – Chris Eidhof
import Foundation
typealias JSONDict = [String: Any]
struct Parameter<A> {
let apiDescription: String
let parse: (Any) -> A?
}
extension Parameter {
@zyuanming
zyuanming / Future
Last active June 15, 2017 02:25
Applicatives and Swift – Stephen Celis
import Foundation
import Dispatch
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
precedencegroup Additive {
associativity: left
}