Skip to content

Instantly share code, notes, and snippets.

// If we extend UnicodeScalar to conform to ForwardIndexType, we can create and map a Range.
extension UnicodeScalar: ForwardIndexType {
public func successor() -> UnicodeScalar {
return UnicodeScalar(value + 1)
}
}
let alphabet = ("A"..."Z" as Range).map { String($0) }
// ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
// (c) 2015 Nate Cook, licensed under the MIT license
//
// An example of how to use the defer statement to lose friends and confuse people
/// A sequence that scans a collection, using a predicate that indicates split points
/// to group chunks of the collection's elements.
public struct ScanningSequence<Base : CollectionType> : SequenceType, LazySequenceType {
private let base: Base
private let predicate: (Base.Generator.Element) -> Bool
public typealias Generator = AnyGenerator<Base.SubSequence>
@natecook1000
natecook1000 / DictionaryObjectsForKeys.swift
Last active December 9, 2015 18:43
Initializers for Dictionary like NSDictionary +dictionaryWithObjects:forKeys:
// (c) 2015 Nate Cook, licensed under the MIT license
//
// Initializers for Dictionary like NSDictionary +dictionaryWithObjects:forKeys:
public extension Dictionary {
/// Creates a new Dictionary from the given `keys` and `values` collections.
///
/// More efficient than the sequence version because it can reserve the correct
/// capacity before filling the dictionary. Returns `nil` if the lengths of the
/// two collections differ.
@natecook1000
natecook1000 / operatorCharacters.swift
Last active September 2, 2024 22:07
Allowed characters for Swift operators
import Foundation
extension UnicodeScalar : ForwardIndexType {
public func successor() -> UnicodeScalar {
return UnicodeScalar(value + 1)
}
}
var operatorHeads: [UnicodeScalar] = Array("=-+!*%<>&|^~?".unicodeScalars)
operatorHeads += Array("\u{00A1}" ... "\u{00A7}")
/// A collection that wraps a dictionary's keys to provide fast
/// checking for whether a key exists.
public struct DictionaryKeysCollection<Key: Hashable, Value>: CollectionType {
private let _base: Dictionary<Key, Value>
public init(_ base: Dictionary<Key, Value>) {
self._base = base
}
/// Returns true if the base dictionary contains the given key.
@natecook1000
natecook1000 / modifiedCopy.swift
Last active October 29, 2015 19:24 — forked from anonymous/modifiedCopy.swift
turn foo.xInPlace(arg) into a foo.x(arg)...
func modifiedCopy<Struct, Arg>(var start: Struct, @noescape mutator: (inout Struct) -> Arg -> (), arg: Arg) -> Struct {
mutator(&start)(arg)
return start
}
extension Array {
func arrayByAppending(e: Element) -> Array {
return modifiedCopy(self, mutator: Array.append, arg: e)
}
}
@natecook1000
natecook1000 / generics_playground.swift
Last active June 2, 2016 23:27 — forked from jessesquires/generics_playground.swift
Swift optional generic parameters?
protocol FactoryAType {
typealias Product
}
protocol FactoryBType {
typealias Product
}
struct CombinedFactory<T: FactoryAType, U: FactoryBType where T.Product == U.Product> {
let factoryA: T
@natecook1000
natecook1000 / NSDate+Comparable.swift
Last active November 17, 2015 22:00
Making NSDate Comparable
import Foundation
extension NSDate: Comparable { }
public func ==(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == .OrderedSame
}
public func <(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == .OrderedAscending
@natecook1000
natecook1000 / TypeAndProtocol.swift
Created November 23, 2015 22:44
Example: typed variable that also conforms to a protocol
// (c) 2015 Nate Cook, licensed under the MIT license
//
// Example: typed variable that also conforms to a protocol
import Foundation
// A protocol that can be used as a type (no Self, no typealias)
public protocol NamedObjectType {
var name: String { get }
}
@natecook1000
natecook1000 / nshipster-new-years-2016.md
Last active July 10, 2018 19:24
NSHipster New Year's 2016

Greetings and salutations, NSHipsters!

As the year winds down, it's a tradition here at NSHipster to ask you, dear readers, to offer up your favorite tricks and tips from the past year as gifts to your fellow hipsters. With iOS 9, El Capitan, brand new watch- and tvOS's, and the open-sourcing of some minor Apple-related tech, there's bound to be lots to share.

Submit your favorite piece of Swift or @objc trivia, helpful hints, unexpected discoveries, useful workarounds, useless fascinations, or anything else you found cool this year. Just comment below!

If you need inspiration, try [the list from last year][2015], or [from the year before][2014], or [from the year before that][2013].