Skip to content

Instantly share code, notes, and snippets.

View NachoSoto's full-sized avatar

NachoSoto NachoSoto

View GitHub Profile
@NachoSoto
NachoSoto / UIAccessibilityTraits+Extensions.swift
Last active March 19, 2019 23:10
Extensions to simplify usage of accessibilityTraits in Swift 4.2:
// Extensions to simplify usage of accessibilityTraits in Swift 4.2:
public func |= (traits: inout UIAccessibilityTraits, other: [UIAccessibilityTraits]) {
for o in other {
traits |= o
}
}
public func |= (traits: inout UIAccessibilityTraits, other: UIAccessibilityTraits) {
// From: https://swift.org/migration-guide-swift4.2/#known-migration-issues
@NachoSoto
NachoSoto / UniqueElements.swift
Created December 27, 2017 01:28
Example of how to extract unique elements from a sequence in Swift
import Foundation
extension Sequence {
// Extension on Sequence allows us to use this method on Arrays, Maps, Sets, etc.
func uniqueElements<Index: Hashable>(for indexer: (Element) -> Index) -> [Element] {
// Keep track of what we've already seen in a Set,
// which allows us to query for elements efficiently.
var seenElements: Set<Index> = []
var result: [Element] = []
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
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
@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.
//
// Signal+Extensions.swift
// Khan Academy
//
// Created by Nacho Soto on 10/1/15.
// Copyright © 2015 Khan Academy. All rights reserved.
//
import ReactiveCocoa
@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) {
# 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
var testArray: [Int] = []
func modify(action: [Int] -> [Int]) {
testArray = action(testArray)
}
for _ in 0..<50 {
modify { array in
var array = array
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