Skip to content

Instantly share code, notes, and snippets.

@khanlou
khanlou / A - Usage.swift
Last active April 26, 2024 05:05
This ScrollView has a modifier called `onScroll`, which is updated when scrolls occur.
struct ContentView: View {
@State var scrollOffset: CGPoint = .zero
var body: some View {
ObservableScrollView {
Text("Hello, world!")
.foregroundColor(self.scrollOffset.y == 0 ? .blue : .red)
}
.onScroll { self.scrollOffset = $0 }
public enum NilOrdering {
case first, last
}
public enum SortOrdering {
case ascending, descending
}
public extension Sequence {
func sorted(on accessor: (Element) -> some Comparable, ordering: SortOrdering = .ascending) -> [Element] {
@khanlou
khanlou / Fonts.swift
Created October 6, 2016 21:10
Print all fonts in Swift 3
UIFont.familyNames.forEach({ familyName in
let fontNames = UIFont.fontNames(forFamilyName: familyName)
print(familyName, fontNames)
})
import Foundation
import Security
private let DataKey = "MyDataKey"
/**
* The KeychainResult class wraps an OSStatus that is returned from a keychain action. It vends KeychainErrors.
*/
struct KeychainResult {
let keychainError: OSStatus
extension BidirectionalCollection {
func makeBidirectionalIterator() -> BidirectionalIterator<Self, Index> {
return BidirectionalIterator<Self, Index>(collection: self)
}
}
let a = [1, 2, 3, 4]
import AsyncHTTPClient
import Foundation
struct ServerSentEvent {
let name: String
let data: String
init(_ substring: Substring) {
let lines = substring.split(whereSeparator: \.isNewline)
self.name = String(lines
//
// ContentView.swift
// DependentEnvironment
//
// Created by Soroush Khanlou on 3/27/23.
//
import SwiftUI
struct Inner: View {
struct BundleVersion: Comparable {
let tuple: (Int, Int, Int)
init(string: String) {
let numbers = string.split(separator: ".").compactMap({ Int($0) })
let major = numbers.count >= 1 ? numbers[0] : 0
let minor = numbers.count >= 2 ? numbers[1] : 0
let patch = numbers.count >= 3 ? numbers[2] : 0
@khanlou
khanlou / StateMachine.swift
Created October 21, 2016 00:14
A state machine where invalid transitions can't compile
protocol State {}
struct Transition<From: State, To: State> {
let transition: () -> To
}
struct Machine<CurrentState: State> {
var state: CurrentState
@khanlou
khanlou / SKSerialInputStream.h
Last active November 10, 2022 11:41
Partially adapted from AFMultipartBodyStream
//
// SKSerialInputStream.h
// inputstream
//
// Created by Soroush Khanlou on 11/4/18.
// Copyright © 2018 Soroush Khanlou. All rights reserved.
//
#import <Foundation/Foundation.h>