Skip to content

Instantly share code, notes, and snippets.

View NachoSoto's full-sized avatar

NachoSoto NachoSoto

View GitHub Profile
enum A {
case A1
}
enum B {
case B1
var a: A {
return .A1
}
}
// Compile with `swiftc switch.swift`
enum A {
case A(Int)
case B(Bool)
case C
case D
}
enum B {
func f(a: Bool, b: Bool) {
switch (a, b) {
case (false, false):
break
case (false, true):
break
case (true, false):
break
case (true, true):
break
var testArray: [Int] = []
func modify(action: [Int] -> [Int]) {
testArray = action(testArray)
}
for _ in 0..<50 {
modify { array in
var array = array
# Using https://github.com/Anviking/Decodable as example:

xcodebuild -sdk appletvsimulator -project Decodable.xcodeproj -configuration Release -scheme Decodable-tvOS ONLY_ACTIVE_ARCH=NO clean build
# This fails with http://www.openradar.me/22993940
@NachoSoto
NachoSoto / AnyValidator.swift
Last active December 3, 2015 14:21
Type-erased ValidatorType
protocol ValidatorType {
typealias ValidatedType
func isValid(object: ValidatedType) -> Bool
}
// You might want to make it a reference type, depending on your case.
struct AnyValidator<T>: ValidatorType {
private let validatorBlock: (T) -> Bool
init<V: ValidatorType where V.ValidatedType == T>(validator: V) {
//
// Signal+Extensions.swift
// Khan Academy
//
// Created by Nacho Soto on 10/1/15.
// Copyright © 2015 Khan Academy. All rights reserved.
//
import ReactiveCocoa
@NachoSoto
NachoSoto / signal-collect
Created February 22, 2016 04:17
Converting [SignalProducer<U>] -> SignalProducer<[U]>
// https://twitter.com/davedelong/status/701621040015810560: @NachoSoto is there a way to take a # of SignalProducer<U,E> and turn them in to SignalProducer<[U], E>? Like combineLatest but w/o tuples
let signals: [SignalProducer<T, E>]
let result: SignalProducer<[T], E> =
SignalProducer(values: signals) // create a producer that emits all signals.
.flatten(.Merge) // merge all values that they emit.
.collect() // collect all values into an array.
Test Suite 'All tests' started at 2017-03-28 11:27:46.053
Test Suite 'Quick.framework' started at 2017-03-28 11:27:46.055
Test Suite 'Quick.framework' passed at 2017-03-28 11:27:46.055.
Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.001) seconds
Test Suite 'APITests.xctest' started at 2017-03-28 11:27:46.056
Test Suite 'WebURLParserSpec' started at 2017-03-28 11:27:46.057
Test Case '-[APITests.WebURLParserSpec Card_graph_URLs__parses_url_with_all_data]' started.
=================================================================
==32504==ERROR: AddressSanitizer: heap-use-after-free on address 0x60600028a8b0 at pc 0x0001207f1970 bp 0x7fff529ebf30 sp 0x7fff529ebf28
WRITE of size 8 at 0x60600028a8b0 thread T0
import UIKit
import Cartography // https://github.com/robb/Cartography
/**
This is an example of self sizing `UICollectionView` cells using AutoLayout,
where the width of cells is always the width of the parent, to mimic `UITableView`.
*/
fileprivate let items: [String] = (0..<100)
.map { _ in Lorem.sentences(Int.random(min: 1, max: 8)) } // Using https://github.com/lukaskubanek/LoremSwiftum/blob/master/Sources/LoremSwiftum.swift