Skip to content

Instantly share code, notes, and snippets.

View pofat's full-sized avatar

Pofat pofat

View GitHub Profile
@pofat
pofat / Kind.swift
Created March 12, 2021 15:49
Swift Struct Metadata
// Kind of type
public enum Kind {
case `struct`
case `enum`
case optional
case opaque
case tuple
case function
case existential
case metatype
@pofat
pofat / LaunchTracker.swift
Created February 22, 2023 02:10
Get permain and check if it's prewarm
/// iOS 15+
func isPrewarm() -> Bool {
if let prewawrmEnv = ProcessInfo.processInfo.environment["ActivePrewarm"],
prewawrmEnv == "1" {
return true
} else {
return false
}
}
@pofat
pofat / Fastfile
Last active March 26, 2023 01:58
Shell script and Fastfile to build universal framework.
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 / generic_and_protocol.swift
Created August 6, 2019 11:56
兩種宣告方式之間有什麼差異?
// 會帶真實型別進去
func doSomething<T: MyPotocol>(_ arg: T) { }
// 有 existentail container
func doSomething(_ arg: MyProtofcol) { }
@pofat
pofat / closure.swift
Created September 29, 2019 08:16
Discuss how closure capturing and capture list works
import Foundation
// struct for printing out instance address
struct MemoryAddress<T>: CustomStringConvertible {
let intValue: Int
var description: String {
let length = 2 + 2 * MemoryLayout<UnsafeRawPointer>.size
return String(format: "%0\(length)p", intValue)
}
@pofat
pofat / asyncstream.swift
Created August 16, 2022 08:24
Delegate with AsyncStream
protocol FooDelegate: AnyObject {
func didReceive(value: Int)
func didOpen()
func didClose()
}
class Foo {
weak var delegate: FooDelegate?
func open() {
@pofat
pofat / saerv.swift
Created August 15, 2022 09:52
TCA state-action-env-reducer-view code snippet
import ComposableArchitecture
import SwiftUI
struct <#Name#>State: Equatable {}
enum <#Name#>Action: Equatable {}
struct <#Name#>Environment {}
extension <#Name#>Environment {
@pofat
pofat / DelegatePublisher.swift
Last active August 9, 2022 04:42
How to handle delegate in a reactive way? Turn a delegate into Publisher.
import Combine
import CombineExt // Check this implementation: https://github.com/CombineCommunity/CombineExt/blob/main/Sources/Operators/Create.swift
import CoreBluetooth
// Client to generate target delegate publisher
// Learned from https://github.com/pointfreeco/composable-core-location/blob/main/Sources/ComposableCoreLocation/Live.swift
struct PeripheralClient {
enum Action: Equatable {
case didUptateName
@pofat
pofat / crash.swift
Created July 24, 2022 21:36
Code that crash Swift compiler (before 5.8)
protocol P {
associatedtype A
associatedtype B
}
protocol Q: P {}
extension Q where A == Int {
typealias B = Int
}
@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)