Skip to content

Instantly share code, notes, and snippets.

View DevAndArtist's full-sized avatar
🦅
Hacking in Swift

Adrian M. DevAndArtist

🦅
Hacking in Swift
View GitHub Profile
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
// we generate the boundary with `A | B` or directly OneOf<A, B>
enum OneOf<...T> {
...case $#(T)
// Bikeshedding variadic enum casses:
// we might need an index to set the value?
init(index: Int, value: T) {
self = .$index(value)
//
// 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
DevAndArtist / ForDevice.swift
Created August 26, 2016 18:36
A function to assign device specific value for UIKit.
// Swift 3.0:
// This functuion makes use of the custom operator `?!`:
// https://gist.github.com/DevAndArtist/dad641ee833e60b02fd1db2dbb488c6a
@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
DevAndArtist / endian-conversion-cheat-sheet.swift
Last active April 4, 2024 03:48
A cheat sheet for byte conversion of number types in Swift 3.0
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))
}
}
final class Storage {
// not empty for testing
var keys: [String] = ["0"]
var values: [Int] = [0]
}
public struct Document {
var _storageReference: Storage

ContainerViewController

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.

Todo

  • Mimic UINavigationController without any bar APIs
  • More flexible delegate methods
  • Simple non-interactive transition
  • Interactive transition
/// 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