Skip to content

Instantly share code, notes, and snippets.

Avatar

Pofat pofat

View GitHub Profile
@pofat
pofat / asyncstream.swift
Created Aug 16, 2022
Delegate with AsyncStream
View asyncstream.swift
protocol FooDelegate: AnyObject {
func didReceive(value: Int)
func didOpen()
func didClose()
}
class Foo {
weak var delegate: FooDelegate?
func open() {
@pofat
pofat / saerv.swift
Created Aug 15, 2022
TCA state-action-env-reducer-view code snippet
View saerv.swift
import ComposableArchitecture
import SwiftUI
struct <#Name#>State: Equatable {}
enum <#Name#>Action: Equatable {}
struct <#Name#>Environment {}
extension <#Name#>Environment {
@pofat
pofat / DelegatePublisher.swift
Last active Aug 9, 2022
How to handle delegate in a reactive way? Turn a delegate into Publisher.
View DelegatePublisher.swift
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 Jul 24, 2022
Code that crash Swift compiler (before 5.8)
View crash.swift
protocol P {
associatedtype A
associatedtype B
}
protocol Q: P {}
extension Q where A == Int {
typealias B = Int
}
@pofat
pofat / Either.swift
Created Nov 14, 2019
Codable Either
View Either.swift
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)
@pofat
pofat / bytes_handler.swift
Created Nov 1, 2016
A method to retrieve byte array and bit array from Data in Swift 3
View bytes_handler.swift
// Using UInt8 as Byte
typealias Byte = UInt8
enum Bit: Int {
case zero, one
}
extension Data {
var bytes: [Byte] {
var byteArray = [UInt8](repeating: 0, count: self.count)
self.copyBytes(to: &byteArray, count: self.count)
@pofat
pofat / Kind.swift
Created Mar 12, 2021
Swift Struct Metadata
View Kind.swift
// Kind of type
public enum Kind {
case `struct`
case `enum`
case optional
case opaque
case tuple
case function
case existential
case metatype
View View.swift
struct Collapsable<Content: View>: View {
@State var collapsed: Bool = true
let content: () -> Content
init(@ViewBuilder content: @escapin () -> Content) {
self.content = content
}
var body: some View {
VStack {
@pofat
pofat / filter_keyPath.swift
Created May 22, 2022
Filter with KeyPath
View filter_keyPath.swift
struct Foo {
var bar: Int
var overThreshold: Bool {
bar > 5
}
}
let fooArray: [Foo] = ...
let filtered = fooArray.filter(\.overThreshold)
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]]