Skip to content

Instantly share code, notes, and snippets.

Hacking in Swift

Adrian M. DevAndArtist

Hacking in Swift
View GitHub Profile
View NSLayoutConstraintSearchable.swift
import UIKit
protocol NSLayoutConstraintSearchable: class {}
extension NSLayoutConstraintSearchable {
// this function does add support for UIView and UILayoutGuide
private func getConstraints() -> [NSLayoutConstraint]? {
// only UIView's hold constraints
View OneOf.swift
// we generate the boundary with `A | B` or directly OneOf<A, B>
enum OneOf<...T> { $#(T)
// Bikeshedding variadic enum casses:
// we might need an index to set the value?
init(index: Int, value: T) {
self = .$index(value)
View UnwrapOrTrap.swift
// UnwrapOrTrap.swift
infix operator ?! : NilCoalescingPrecedence
/// Performs a nil-coalescing operation, returning the wrapped value of an
/// `Optional` instance or uses the rhs function to stop the program.
/// - Parameters:
DevAndArtist / ForDevice.swift
Created Aug 26, 2016
A function to assign device specific value for UIKit.
View ForDevice.swift
// Swift 3.0:
// This functuion makes use of the custom operator `?!`:
@available(iOS 9.0, *)
func forDevice<T>(
phone: @autoclosure () -> T? = nil,
pad: @autoclosure () -> T? = nil,
tv: @autoclosure () -> T? = nil,
carPlay: @autoclosure () -> T? = nil) -> T {
DevAndArtist / endian-conversion-cheat-sheet.swift
Last active Nov 13, 2021
A cheat sheet for byte conversion of number types in Swift 3.0
View endian-conversion-cheat-sheet.swift
func _convertToBytes<T>(_ value: T, withCapacity capacity: Int) -> [UInt8] {
var mutableValue = value
return withUnsafePointer(to: &mutableValue) {
return $0.withMemoryRebound(to: UInt8.self, capacity: capacity) {
return Array(UnsafeBufferPointer(start: $0, count: capacity))
View mutable-view.swift
final class Storage {
// not empty for testing
var keys: [String] = ["0"]
var values: [Int] = [0]
public struct Document {
var _storageReference: Storage


A trully custom container view controller written in Swift 3.0.1. The view of this object is fully resizable and works great with layout constraints.


  • Mimic UINavigationController without any bar APIs
  • More flexible delegate methods
  • Simple non-interactive transition
  • Interactive transition
View objc_runtime_swiftification_sketch.swift
/// An opaque type that represents a method in a class definition.
public struct Method {
public struct Description {
/**< The name (selector) of the method */
public var selector: Selector
/**< The types of the method arguments */
public var types: UnsafeMutablePointer<Int8>
Path Protocol
/swift/stdlib/public/core/AnyHashable.swift:16 _HasCustomAnyHashableRepresentation
/swift/stdlib/public/core/BidirectionalCollection.swift:21 _BidirectionalIndexable
/swift/stdlib/public/core/BridgeObjectiveC.swift:19 _ObjectiveCBridgeable
/swift/stdlib/public/core/Collection.swift:20 _IndexableBase
/swift/stdlib/public/core/Collection.swift:176 _Indexable
/swift/stdlib/public/core/CompilerProtocols.swift:193 _ExpressibleByBuiltinIntegerLiteral
/swift/stdlib/public/core/CompilerProtocols.swift:240 _ExpressibleByBuiltinFloatLiteral
/swift/stdlib/public/core/CompilerProtocols.swift:283 _ExpressibleByBuiltinBooleanLiteral
Suggestion 1 Suggestion 2 Suggestion 3 Swift 3 Implication of #3
public open open public open open
closed public public public public public
fixed public  
final public closed final public final public final public
internal internal internal internal internal
final internal final internal final internal final internal final internal
private private private fileprivate private
final private final private final private final fileprivate final private