Skip to content

Instantly share code, notes, and snippets.

View bsneed's full-sized avatar

Brandon Sneed bsneed

View GitHub Profile
@bsneed
bsneed / gist:ec60487ce79492263ac255a9032387e9
Created December 9, 2023 23:51
Swift - Store properties in an extension
import Foundation
internal class StoredExtensionPropertyData {
internal static var storedExtensionProperties = [ObjectIdentifier: StoredExtensionPropertyData]()
var properties = [String: Any]()
}
internal protocol ExtensionProperties: AnyObject {
func get<T>(key: String, owner: ExtensionProperties) -> T?
func set<T>(key: String, value: T?, owner: ExtensionProperties)
@bsneed
bsneed / middleware.swift
Created May 18, 2020 19:32
Segment Middleware Example
let checkConsentOfEvent = SEGBlockMiddleware { (context, next) in
if context.eventType == .track {
if (hasConsent(for: blah)) {
next(context)
} else {
return
}
} else {
next(context)
}
@bsneed
bsneed / bridging.swift
Created January 15, 2020 02:39
An example of bridging, and mutable/immutable class sets.
public struct AContext: ReferenceConvertible, Equatable, Hashable {
internal var _handle: SEGMutableContext
public typealias ReferenceType = SEGContext
public typealias _ObjectiveCType = SEGContext
public static func _getObjectiveCType() -> Any.Type {
return SEGContext.self
}
@bsneed
bsneed / Thing.swift
Created January 7, 2017 19:20
Less high level....
public struct MessagesState: State, HasMessagesUnread, HasMessagesRead {
public var unreadCount: UInt = 0
public var messages = [String]()
public var outbound = [String]()
public var sentCount: UInt = 0
public var readCount: UInt = 0
}
// supported state manipulation protocols.
@bsneed
bsneed / Conformance.swift
Created January 7, 2017 18:41
Swift-test: Protocol conformance and generics test
protocol Proto1 {
var someInt: Int { get set }
}
protocol Proto2 {
var someInt2: Int { get set }
}
struct AStruct: Proto1, Proto2 {
var someInt: Int = 0
@bsneed
bsneed / exceptionFailure.swift
Created November 4, 2016 18:07
Exception failure in swift.
public func exceptionFailure(_ message: String) {
let args: [CVarArg] = []
if isInUnitTest() {
NSException.raise(NSExceptionName(rawValue: ELExceptionFailure), format: message, arguments: getVaList(args))
} else {
#if DEBUG
NSException.raise(NSExceptionName(rawValue: ELExceptionFailure), format: message, arguments: getVaList(args))
#endif
}
}
@bsneed
bsneed / Timings.txt
Last active January 9, 2016 17:29
Performance Comparison of Swift JSON->Model frameworks.
Time to decode 10,000 Person struct's from JSON:
Argo (Simple): 8.563 seconds
measureBlock {
let _ : [Person]? = decode(data)
}
Argo (Decomp'd): 3.344
measureBlock {
let json: Argo.JSON = JSON.parse(data)
@bsneed
bsneed / Coconut_Test.swift
Created November 16, 2015 17:56
Some unit test code to compare coconut against Argo and By_Hand decoding.
func test_measure_coconut() {
let json = THGCodable.JSON(bundleClass: TestingStuffTests.self, filename: "test.json")
measureBlock {
let array = json?.array
if let array = array {
let _: [Person] = array.map { object in
let jsonPerson = object as THGCodable.JSON
return try! Person.decode(jsonPerson)
}
@bsneed
bsneed / Decimal.swift
Created November 8, 2015 17:58
Treat y0 NSDecimalNumber's like first class peeps instead of red-headed-step-children, in Swift.
//
// Decimal.swift
// Decimal
//
// Created by Brandon Sneed on 11/7/15.
// Copyright © 2015 theholygrail.io. All rights reserved.
//
import Foundation
@bsneed
bsneed / testNSDecimalNumberWrapper.swift
Created November 7, 2015 22:03
Decimal is a struct that wraps NSDecimalNumber so you can use it like other types (Float, Int, Double, Int64, etc)
func testSomeStuff() {
// trying to come up with shit to test.
var d: Decimal = 1234
print(d)
d += 2
print(d)