Skip to content

Instantly share code, notes, and snippets.

View pofat's full-sized avatar

Pofat pofat

View GitHub Profile
@pofat
pofat / Sequence.swift
Created May 19, 2019 16:06
Definition of Sequence protocl
protocol Sequence {
associatedtype Element
associatedtype Iterator : IteratorProtocol where Iterator.Element == Element
func makeIterator() -> Iterator
}
@pofat
pofat / Requests.swift
Last active May 19, 2019 04:29
Implement basic network request with both protocol and generic struct
import Foundation
import RxSwift
enum HTTPMethod: String {
case POST, GET
}
// An object
@pofat
pofat / GetCreationTime.swift
Last active June 20, 2019 17:20
Get the timestamp of creation time of the process which runs your application in iOS
func getProcessStartTime() -> TimeInterval {
let pid = ProcessInfo.processInfo.processIdentifier
var procInfo = kinfo_proc()
var cmd: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, pid]
var size = MemoryLayout.stride(ofValue: procInfo)
if sysctl(&cmd, UInt32(cmd.count), &procInfo, &size, nil, 0) == 0 {
// tv_sec is timestamp measured in second; tv_usec is the rest fraction part in microsecond
return Double(procInfo.kp_proc.p_un.__p_starttime.tv_sec) * 1000.0 + Double(procInfo.kp_proc.p_un.__p_starttime.tv_usec) / 1000.0
} else {
print("Can't get information of process \(pid)")
@pofat
pofat / RxRequests.swift
Created May 4, 2019 15:26
Rx + Network Request
import Foundation
import RxSwift
enum HTTPMethod: String {
case POST, GET
}
// protocol of request, should define endpoint, http method, params and response type
protocol Request {
var path: String { get }
@pofat
pofat / ocmock-cheatsheet.m
Created December 20, 2018 14:59 — forked from oks/ocmock-cheatsheet.m
OCMock cheatsheet
/*----------------------------------------------------*/
#pragma mark - XCTAsserts
/*----------------------------------------------------*/
XCTAssert(expression, format...);
XCTAssertTrue(expression, format...);
XCTAssertFalse(expression, format...);
XCTAssertEqual(expression1, expression2, format...);
XCTAssertNotEqual(expression1, expression2, format...);
XCTAssertNil(expression, format...);
@pofat
pofat / MapDidDrag_RAC.swift
Last active December 11, 2018 14:47
Use RAC to detect first drag after the map is loaded
import ReactiveSwift
import Result
class MapViewController: UIViewController {
private let (didLoadSingal, didLoadObserver) = Signal<Bool, NoError>.pipe()
private let (didDragSignal, didDragObserver) = Signal<Bool, NoError>.pipe()
override func viewDidLoad() {
super.viewDidLoad()
@pofat
pofat / MapDidDrag.swift
Created December 11, 2018 14:40
How to detect the first drag after map is loaded
class MapViewController: UIViewController {
var isLoaded = false
}
extension MapViewController: MKMapViewDelegate {
func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
if isLoaded {
mapDidDrag()
}
}
@pofat
pofat / then.swift
Last active November 7, 2018 12:43
Better way to do initialization in Swift
// From https://github.com/devxoul/Then/blob/master/Sources/Then/Then.swift
// It's easy to move them into your codebase
import Foundation
public protocol Then {}
extension Then where Self: Any {
/// Makes it available to set properties with closures just after initializing.
///
@pofat
pofat / UIView+Extension.swift
Created January 5, 2018 09:10
Better way of adding subviews at once.
/*
From: https://twitter.com/johnsundell/status/948513364015288320?utm_content=buffer67c28&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer
*/
extension UIView {
func add(_ subviews: UIView...) {
subviews.forEach(addSubview)
}
}
extension Float {
var toInt32: Int32 {
// Directly bitwise copy and expressed as Int32
return Int32(bitPattern: self.bitPattern)
}
}
lef f1 = Float(338_556)
let f2 = f1 + f1.ulp