Skip to content

Instantly share code, notes, and snippets.

@wleii
wleii / Asable.swift
Created April 27, 2018 10:04
easy use as/is function at function-chain
protocol Asable {
func `as`<T>(_ anyType: T.Type) -> T?
func `is`<T>(_ anyType: T.Type) -> Bool
}
extension Asable {
func `as`<T>(_ anyType: T.Type) -> T? {
return self as? T
}
func `is`<T>(_ anyType: T.Type) -> Bool {
return self is T
@wleii
wleii / error.swift
Created November 9, 2017 11:08
ERRORs
struct Model {
lazy var value: String = {
return text
}()
var myValue: String {
return value //error: Cannot use mutating getter on immutable value: 'self' is immutable
}
}
@wleii
wleii / Codable.swift
Created October 31, 2017 11:56
Some thoughts about Swift Codable
public extension Decodable {
static func toModel(_ value: [String: Any], using decoder: JSONDecoder = JSONDecoder()) throws -> Self {
let data = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions.init(rawValue: 0))
return try toModel(data, using: decoder)
}
static func toModel(_ value: [[String: Any]], using decoder: JSONDecoder = JSONDecoder()) throws -> [Self] {
let data = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions.init(rawValue: 0))
return try toModel(data, using: decoder)
}
@wleii
wleii / Network.swift
Last active October 31, 2017 09:19
Swift: a top-level design for network request, base Alamofire
import Foundation
import Alamofire
enum HTTPResult<Value> {
case success(Value)
case failure(Error)
}
@wleii
wleii / gist:c8cf3a7ad0800bc19fd12de9fdda118e
Created March 8, 2017 08:04
repeat refresh safari use applescript
#!/usr/bin/osascript
tell application "System Events"
repeat
if exists process "Safari" then
tell application "Safari"
activate
end tell
tell process "Safari"
keystroke "r" using {command down}
@wleii
wleii / gist:bed901c66ab7fec29878eea1a4bd79b7
Last active October 26, 2017 08:02
Resolution gesture ( UIPanGestureRecognizer and UIScreenEdgePanGestureRecognizer ) conflict
extension UIScrollView {
//当UIScrollView滑动手势与 UIScreenEdgePanGestureRecognizer手势同在,手势沿响应链向下传递
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// 如果侧滑 Back 手势以及侧滑滚动手势同时响应,则调用 `open func require(toFail otherGestureRecognizer: UIGestureRecognizer)`使其侧滑滚动手势 failed
if gestureRecognizer is UIPanGestureRecognizer && otherGestureRecognizer is UIScreenEdgePanGestureRecognizer {
gestureRecognizer.require(toFail: otherGestureRecognizer)
// 允许手势沿响应链向下传递
return true
}
let page = Int(floor(($0.x - itemWidth / 2) / itemWidth) + 1)
pageControl.currentPage = page
// MARK: - FlowLayout
private class FlowLayout: UICollectionViewFlowLayout, WaterfullFlowLayout {
var preparedLayoutAttributes: [UICollectionViewLayoutAttributes] = []
override init() {
super.init()
callInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
@wleii
wleii / gist:93d7ce78a476ecb309b5
Created February 24, 2016 06:40
A Simple TableViewCell Appear Animation
override func viewWillAppear(animated: Bool) {
animateTable()
}
func animateTable() {
self.tableView.reloadData()