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)
@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 {
//
// 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:
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>
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
struct A {

  bridge bridge_name {
    
    func foo() {}
  }
}

let a = A()

Vectors

  • A vector contains at least one type.
  • vector T - is a list of type T of an arbitrary length, such as T1, T2, ...
  • vector(n) T - is a list with the length of maximal n T's, such as T1, T2, ... , Tn
// vectorized tuple with arbitrary length (like an existetial that can accept a tuple of type `T` of any length)
var t1: (vector Int) = (1, 2)
t1 = (1, 2, 3)