Skip to content

Instantly share code, notes, and snippets.

@natecook1000
natecook1000 / randomInt.swift
Created October 9, 2014 16:43
Random integers without shuttling between UInt32 and Int
func arc4random_uniform<T: SignedIntegerType>(max: T) -> T {
let max32: Int32 = numericCast(max)
return T(Int64(arc4random_uniform(UInt32(max32))))
}
func arc4random_uniform<T: UnsignedIntegerType>(max: T) -> T {
let max32: UInt32 = numericCast(max)
return T(UInt64(arc4random_uniform(max32)))
}
@natecook1000
natecook1000 / Mesozoic.swift
Created November 3, 2014 21:05
Using ObjC-defined NS_OPTIONS in Swift
// objective-c:
typedef NS_OPTIONS(NSInteger, MesozoicPeriod) {
MesozoicPeriodTriassic,
MesozoicPeriodJurassic,
MesozoicPeriodCretaceous
};
// Swift
let period: MesozoicPeriod = MesozoicPeriod.Jurassic
let period2 = MesozoicPeriod.Jurassic | MesozoicPeriod.Cretaceous
@natecook1000
natecook1000 / openRanges.swift
Last active May 27, 2021 19:37
Open-ended range operators for Swift
// Open-ended range operators
//
// 100... is equivalent to 100...Int.max
// ...-100 is equivalent to Int.min...-100
// ..<3 is equivalent to Int.min..<3
import Swift
/// Conforming types provide static `max` and `min` constants.
protocol MinMaxType {
@natecook1000
natecook1000 / curryRecipe.swift
Created November 9, 2014 03:37
Automatically write a function currying function with many arguments
// Curry Recipe
func curryRecipe(n: Int) -> String {
let types = join(", ", map(1...n, { "T\($0)" }))
let returnType = join(" -> ", map(1...n, { "T\($0)" }))
let closures = join(" in ", map(1...n, { "{ t\($0)" }))
let braces = join(" ", Array(count: n, repeatedValue: "}"))
return "func curry<\(types), R>(f: (\(types)) -> R) -> \(returnType) -> R {\r" +
" return \(closures) in f(\(types.lowercaseString)) \(braces)\r}"
}
@natecook1000
natecook1000 / MyCell.swift
Created November 19, 2014 15:57
Simple UITableViewCell subclass
class MyCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
configureView()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configureView()
@natecook1000
natecook1000 / NSTimer+Closure.swift
Last active October 30, 2025 15:51
Scheduled NSTimer with a Swift closure
extension NSTimer {
/**
Creates and schedules a one-time `NSTimer` instance.
- Parameters:
- delay: The delay before execution.
- handler: A closure to execute after `delay`.
- Returns: The newly-created `NSTimer` instance.
*/
@natecook1000
natecook1000 / shuffle.swift
Last active December 11, 2018 06:53
Shuffle/shuffled functions & Array extensions
// (c) 2014 Nate Cook, licensed under the MIT license
//
// Fisher-Yates shuffle as top-level functions and array extension methods
/// Shuffle the elements of `list`.
func shuffle<C: MutableCollectionType where C.Index == Int>(inout list: C) {
let c = count(list)
for i in 0..<(c - 1) {
let j = Int(arc4random_uniform(UInt32(c - i))) + i
swap(&list[i], &list[j])
// see also https://gist.github.com/griotspeak/8bb4c46611fc90d3043b
func findFirst<S: SequenceType>(seq: S, predicate: S.Generator.Element -> Bool) -> S.Generator.Element? {
for x in seq {
if predicate(x) { return x }
}
return nil
}
func not<T>(predicate: T -> Bool) -> (T -> Bool) {
@natecook1000
natecook1000 / ATPView.swift
Created December 9, 2014 19:59
ATP T-shirt in Swift
// adadpted from http://atp.fm/t-shirt
import UIKit
class ATPView : UIView {
override func drawRect(rect: CGRect) {
let fontName = "MyriadPro-SemiBold"
let title = NSLocalizedString("Accidental Tech Podcast", comment: "title")
let initials = NSLocalizedString("ATP", comment: "initials")
@natecook1000
natecook1000 / NSDate+Sanity.swift
Created December 23, 2014 22:01
Actually sane ways of getting a particular NSDate
// (c) 2014 Nate Cook, licensed under the MIT license
extension NSDate {
/// Initializes an NSDate instance with the given date and time parameters.
convenience init(year: Int, month: Int, day: Int, hour: Int, minute: Int = 0, second: Int = 0) {
var components = NSDateComponents()
components.year = year
components.month = month