Skip to content

Instantly share code, notes, and snippets.

Avatar

Pofat pofat

View GitHub Profile
View demangler.swift
import Foundation
guard CommandLine.arguments.count > 1 else {
print("Please specify object file/executable")
exit(EXIT_FAILURE)
}
var nm = Process()
nm.launchPath = "/usr/bin/nm"
nm.arguments = [CommandLine.arguments[1]]
@pofat
pofat / operator.swift
Created Jan 12, 2022
[Effect<Output, Failure>] -> Effect<[Output], Failure>
View operator.swift
extension Effect {
// Zip with other effects
func zip<Others: Collection>(with others: Others)
-> Effect<[Output], Failure>
where Others.Element == Effect<Output, Failure> {
let first = map { [$0] }
.eraseToEffect()
return others
.reduce(first) { zipped, next in
@pofat
pofat / ProtocolNotofication.swift
Last active Dec 15, 2021
Deal with notification with protocol-oriented programing in Swift
View ProtocolNotofication.swift
//: Playground - noun: a place where people can play
import UIKit
// This is for dmoe, you can use a generice type to limit your observer to an UIViewController for common usage.
protocol Notifiable {
var name: Notification.Name { get }
func observe(by observer: Any, withSelector selector: Selector, object: Any?)
func post(object: Any? ,userInfo: [AnyHashable: Any]?)
static func remove(observer: Any)
@pofat
pofat / scan_unused_selectors.swift
Created Feb 16, 2020
Scan unused ObjC selectors
View scan_unused_selectors.swift
/*
* 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 / Fastfile
Last active Mar 25, 2021
Shell script and Fastfile to build universal framework.
View Fastfile
fastlane_version "2.50.1"
default_platform :ios
platform :ios do
before_all do
# ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
# cocoapods
end
@pofat
pofat / Podfile
Created Feb 16, 2020
Turn your pods into statically linked
View Podfile
# 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 / main.swift
Last active Oct 17, 2020
How to write main function in a Swift iOS project
View main.swift
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
View get_pre_main_time.swift
/// 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 / type_erasure.swift
Last active Feb 17, 2020
如何用 generic struct 消除 PAT 的問題
View type_erasure.swift
// 利用 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 / Dictionary+Extension.swift
Last active Dec 28, 2019
String KeyPath accessor of Dictionary
View Dictionary+Extension.swift
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] {