Skip to content

Instantly share code, notes, and snippets.

View mjarvis's full-sized avatar

Malcolm Jarvis mjarvis

  • British Columbia
View GitHub Profile
import SwiftUI
struct HeroAnimationTest: View {
let indices = 0 ..< 3
@State
var showsOtherView = false
@Namespace
var namespaceID: Namespace.ID
@d4rkd3v1l
d4rkd3v1l / Struct+StoreOnHeap.swift
Last active September 13, 2022 06:34
Swift @propertyWrappers to allocate huge structs on the heap, by wrapping them inside arrays. -> Very effective "hackaround" 😬😎
/// Stores value types on the heap
///
/// Arrays are stored on the heap and only the pointer (8 bytes) to the array remains on the stack.
/// This behaviour is used to wrap another type into an array, and therefore move it to the heap.
///
/// Example usage:
/// ```
/// @StoredOnHeap var hugeStruct: HugeStruct
/// ```
@propertyWrapper
@wtsnz
wtsnz / ContentView.swift
Last active February 27, 2024 15:53
Playing around with hosting SwiftUI Views in an NSWindow from inside a View 🙃 (also works for the NSStatusBar item!)
import SwiftUI
struct ContentView: View {
@State var now = Date()
@State var showWindow = false
@State var text: String = ""
let timer = Timer.publish(every: 1, on: .current, in: .common).autoconnect()
@IanKeen
IanKeen / DictionaryDecoder.swift
Created March 1, 2019 18:43
DictionaryDecoder
import Foundation
class DictionaryDecoder {
init() { }
func decode<T: Decodable>(_ type: T.Type, from data: [String: Any]) throws -> T {
let decoder = _Decoder(codingPath: [], source: data)
return try T(from: decoder)
}
}
@IanKeen
IanKeen / AnyCodingKey.swift
Created March 1, 2019 18:40
AnyCodingKey: Helpful for a range of Codable tricks
struct AnyCodingKey: CodingKey {
var stringValue: String
var intValue: Int?
init?(intValue: Int) {
self.intValue = intValue
self.stringValue = "\(intValue)"
}
init?(stringValue: String) {
self.intValue = nil
@IanKeen
IanKeen / AnyCodable.swift
Last active March 4, 2021 18:45
AnyCodable
public struct AnyCodable: Codable {
public let value: Any?
public init(_ value: Any?) {
self.value = value
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
@IanKeen
IanKeen / NotificationCenter+TypeSafe.swift
Last active December 7, 2022 21:31
Type safe NotificationCenter
struct Notification<T> {
let name: NSNotification.Name
}
private let notificationData = "_notificationData"
extension NotificationCenter {
func post<T>(_ notification: Notification<T>, object: Any? = nil, data: T) {
post(name: notification.name, object: object, userInfo: [notificationData: data])
}
@IanKeen
IanKeen / EncodableDictionary.swift
Last active April 13, 2019 18:37
Type-safe Encodable Dictionary - useful for building quick dynamic Encodable packets
public struct EncodableDictionary {
typealias EncodingFunction = (inout KeyedEncodingContainer<AnyCodingKey>) throws -> Void
// MARK: - Private Properties
private var data: [String: Any] = [:]
private var encodings: [String: EncodingFunction] = [:]
// MARK: - Lifecycle
public init() { }
@Sorix
Sorix / ColorableNavigationController.swift
Created April 12, 2017 14:13
Colourable UINavigationController that supports different colors for navigation bars among different view controllers
//
// ColorableNavigationController.swift
//
// Created by Vasily Ulianov on 26.10.16.
//
import UIKit
/// Navigation bar colors for `ColorableNavigationController`, called on `push` & `pop` actions
public protocol NavigationBarColorable: class {
var navigationTintColor: UIColor? { get }
@loganwright
loganwright / PrioritySort.swift
Created October 22, 2015 20:16
Sort Priorities in Swift
enum SortOrder {
case Ascending, Descending, Same
}
extension Array {
typealias Sorter = (Element, Element) -> SortOrder
func sortWithPriorities(sorters: Sorter...) -> Array {
return sort { (left, right) -> Bool in
for sorter in sorters {
switch sorter(left, right) {