Skip to content

Instantly share code, notes, and snippets.

@Que20
Que20 / dataStructures.Swift
Created February 26, 2016 14:55
Swift Data Structures
// Stack
struct Stack<T> {
var items = [T]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
@Que20
Que20 / imageDarkness.swift
Created July 23, 2019 08:46
Check if an image is dark in Swift.
func imageIsDark(image: CGImage) -> Bool {
let redLum = 0.299
let greenLum = 0.578
let blueLum = 0.114
let maxLum = 150.0
guard let imageData = image.dataProvider?.data else { return false }
guard let ptr = CFDataGetBytePtr(imageData) else { return false }
let length = CFDataGetLength(imageData)
let threshold = Int(Double(image.width * image.height) * 0.45)
var darkPixels = 0
@Que20
Que20 / sortedObject.swift
Created July 23, 2019 09:01
Sorted array of objects.
struct A {
var v: Bool
var s: String
}
var arr = [A(v: false, s: "asd"), A(v: true, s: "zxc"), A(v: true, s: "sdf")]
arr = arr.sorted(by: { $0.v && !$1.v })
print(arr)
@Que20
Que20 / PagerViewController.swift
Created December 7, 2020 13:04
A simple paged view controller implementation.
import Foundation
import UIKit
protocol PagerViewControllerDelegate: class {
func pagerViewController(controller: PagerViewController,
didUpdatePageCount count: Int)
func pagerViewController(controller: PagerViewController,
didUpdatePageIndex index: Int)
}
@Que20
Que20 / EnumBinaryTree.swift
Created December 7, 2020 13:09
An enum based BinaryTree implementation.
enum BinaryTree<Element> {
case leaf
indirect case node(Element, l: BinaryTree<Element>, r: BinaryTree<Element>)
}
extension BinaryTree {
init(_ value: Element) {
self = .node(value, l: .leaf, r: .leaf)
}
}
struct T {
let s: String
let v: Int
}
let a = [T(s: "abc", v: 0), T(s: "abc", v: 1), T(s: "def", v: 0)]
let b = Array(a.reduce([Int: [T]]()) { (d, a) -> [Int: [T]] in
var d = d
let t = a.v
func input(_ block: (UInt8) -> (Bool)) {
func initStruct<S>() -> S {
let struct_pointer = UnsafeMutablePointer<S>.allocate(capacity: 1)
let struct_memory = struct_pointer.pointee
struct_pointer.deallocate()
return struct_memory
}
func enableRawMode(fileHandle: FileHandle) -> termios {
var raw: termios = initStruct()
// A simple network stack I use for my command line tools written in Swift.
// The dataTask here work only for GET operations.
protocol Operation {
associatedtype T: Codable
enum HTTPMethod {
case GET
case POST
case PUT
@Que20
Que20 / NavigationWrapper.md
Last active July 2, 2023 12:39
SwiftUI NavigationWrapper

SwiftUI Navigation Wrapper

Context

I find navigation quite difficult to use in my applied Architecture for SwiftUI project. I made this simpler way to trigger navigation events such as push views and present modals.

Solution

The basic solution I ended up using is this wrapper around NavigationStack. I uses a state variable enum representing the pending navigation operation where it's value changes will trigger the requested operation.

Usage

struct ContentView: View {
    var body: some View {
        NavigationWrapper { navigation in