Skip to content

Instantly share code, notes, and snippets.

View pofat's full-sized avatar

Pofat pofat

View GitHub Profile
@pofat
pofat / get_pre_main_time.swift
Created February 16, 2020 14:23
Get pre-main time
/// Call this at main()
/// return: time in milliseconds
func getPreMainTime() -> Double {
let currentTimeIntervalInMilliSecond = Date().timeIntervalSince1970 * 1000.0
var procInfo = kinfo_proc()
let pid = ProcessInfo.processInfo.processIdentifier
var cmd: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, pid]
var size = MemoryLayout.stride(ofValue: procInfo)
// Retrieve information of current process
if sysctl(&cmd, UInt32(cmd.count), &procInfo, &size, nil, 0) == 0 {
@pofat
pofat / Podfile
Created February 16, 2020 14:13
Turn your pods into statically linked
# Add these in your Podfile (available for CocoaPods v1.7.x+)
# Replace with any Pods that you want to keep dynamically linked
keep_dynamically_linked = ['RxCocoa', 'RxSwift']
target 'YourApp' do
# all you pods here...
end
@pofat
pofat / scan_unused_selectors.swift
Created February 16, 2020 14:00
Scan unused ObjC selectors
/*
* Note: You need to build this script before use
* Usage:
* 1. Build by `swfitc scan_unused_selectors.swift`
* 2. Run by `./scan_unused_selectors /path/to/yourMachO`
*
* How to locate MachO of your APP (`/path/to/yourMachO` in above example)? In your Xcod project navigator, you can find a folder `Products`
* In that folder you can see your app (after any build) and right click on it -> "Show In Finder"
* You can get your APP's location by drag it into your terminal. Say it's "/path/to/MyApp.app", your MachO path would be "/path/to/MyApp.app/MyApp"
*/
@pofat
pofat / main.swift
Last active October 17, 2020 15:09
How to write main function in a Swift iOS project
import Foundation
import UIKit
UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, NSStringFromClass(UIApplication.self), NSStringFromClass(AppDelegate.self))
// Remeber to comment or remove `@UIApplicationMain` in your AppDelegate.swift
@pofat
pofat / Dictionary+Extension.swift
Last active December 28, 2019 22:23
String KeyPath accessor of Dictionary
import Foundation
// Time Complexity: O(N), N stands for the level of key path
extension Dictionary where Key == String {
subscript(keyPath keyPath: String) -> Any? {
get {
guard !keyPath.isEmpty else { return nil }
var value: Any? = self
for key in keyPath.components(separatedBy: ".") {
if let node = (result as? [Key: Any])?[key] {
@pofat
pofat / Either.swift
Created November 14, 2019 15:09
Codable Either
enum Either<A, B> {
case left(A)
case right(B)
}
extension Either: Decodable where A: Decodable, B: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let a = try? container.decode(A.self) {
self = .left(a)

Keybase proof

I hereby claim:

  • I am pofat on github.
  • I am pofat (https://keybase.io/pofat) on keybase.
  • I have a public key ASDG9WSBfhua2AKTJqJhSDqdAuCPFwSEfVnVlhryt9fTVQo

To claim this, I am signing this object:

@pofat
pofat / ORT.swift
Created October 19, 2019 05:15
Use `Opaque Return Type` to erase type.
// 想像以下 function 定義在某一個 module 裡,外部無法決定也不依賴具體的型別 (此 func 你可以呼叫但裡面實作看不到)
func getIterator() -> some IteratorProtocol {
var state = (0, 1)
return MyAnyIterator { () -> Int in
let upcomingNumber = state.0
state = (state.1, state.0 + state.1)
return upcomingNumber
}
}
@pofat
pofat / type_erasure.swift
Last active February 17, 2020 08:58
如何用 generic struct 消除 PAT 的問題
// 利用 init with closure 來自動代入 `Element` 真實的型別,而不是直接用 <某型別> 的方式來指定
// 先宣告一個 generic struct
struct MyAnyIterator<Element> {
// 內部使用的 Box ,裡面就是把 closure 存起來,本身也滿足 IteratorProtocol
private class AnyIteratorBox<Element>: IteratorProtocol {
typealias Base = () -> Element?
private var _base: Base
init(_ base: @escaping Base) {
self._base = base
@pofat
pofat / PAT.swift
Last active October 19, 2019 03:37
PAT 好讀版
// 有 associatedtype 的 protocol
public protocol IteratorProtocol {
associatedtype Element
mutating func next() -> Element?
}
// 有 Self 的 protocol, Hashable 的 Self 來自所繼承的 Equatable
public protocol Hashable: Equatable {
var hashValue: Int { get }
func hash(into hasher: inout Hasher)