Skip to content

Instantly share code, notes, and snippets.

Avatar

NachoSoto NachoSoto

  • San Francisco, California
View GitHub Profile
@NachoSoto
NachoSoto / UIAccessibilityTraits+Extensions.swift
Last active Mar 19, 2019
Extensions to simplify usage of accessibilityTraits in Swift 4.2:
View UIAccessibilityTraits+Extensions.swift
// 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 Dec 27, 2017
Example of how to extract unique elements from a sequence in Swift
View UniqueElements.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] = []
View FullWidthSelfSizingCollectionView.swift
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
View swift-3.1-regression
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 Feb 22, 2016
Converting [SignalProducer<U>] -> SignalProducer<[U]>
View signal-collect
// 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.
View Signal+Extensions.swift
//
// 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 Dec 3, 2015
Type-erased ValidatorType
View AnyValidator.swift
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) {
View tvos.bash
# 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
View memory_crash.swift
var testArray: [Int] = []
func modify(action: [Int] -> [Int]) {
testArray = action(testArray)
}
for _ in 0..<50 {
modify { array in
var array = array
View switch-bool.swift
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
You can’t perform that action at this time.