Skip to content

Instantly share code, notes, and snippets.

@natecook1000
natecook1000 / SimplePickerView.swift
Created January 6, 2015 21:48
Simple UIPickerView subclass
class SimplePickerView : UIPickerView {
class SimplePickerViewModel : NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
var titles: [String]
var selectionHandler: ((pickerView: UIPickerView, row: Int, title: String) -> ())?
init(titles: [String], selectionHandler: ((pickerView: UIPickerView, row: Int, title: String) -> ())? = nil) {
self.titles = titles
self.selectionHandler = selectionHandler
}
@natecook1000
natecook1000 / CalculatorView.swift
Last active June 6, 2022 01:00
An IBInspectable Calculator Construction Set
// CalculatorView.swift
// as seen in http://nshipster.com/ibinspectable-ibdesignable/
//
// (c) 2015 Nate Cook, licensed under the MIT license
/// The alignment for drawing an String inside a bounding rectangle.
enum NCStringAlignment {
case LeftTop
case CenterTop
case RightTop
@natecook1000
natecook1000 / OptionalBinding.swift
Created February 11, 2015 04:15
Some extremely contrived examples using Swift 1.2 if-let bindings
// OptionalBinding.swift
// as seen in http://nshipster.com/swift-1.2/
//
// (c) 2015 Nate Cook, licensed under the MIT license
let a: Int? = 10
let b: Int? = 5
let c: Int? = 3
let d: Int? = -2
let e: Int? = 0
@natecook1000
natecook1000 / NSScanner+Swift.swift
Created March 3, 2015 20:13
Swift-friendly NSScanner methods
// NSScanner+Swift.swift
// A set of Swift-idiomatic methods for NSScanner
//
// (c) 2015 Nate Cook, licensed under the MIT license
import Foundation
extension NSScanner {
// MARK: Strings
// integerWithBytes.swift
// as seen in http://natecook.com/blog/2015/03/a-sanatorium-for-swift-generics/
//
// (c) 2015 Nate Cook, licensed under the MIT license
protocol BitshiftOperationsType {
func <<(lhs: Self, rhs: Self) -> Self
func >>(lhs: Self, rhs: Self) -> Self
func <<=(inout lhs: Self, rhs: Self)
func >>=(inout lhs: Self, rhs: Self)
@natecook1000
natecook1000 / NSCalendar+Swift.swift
Created March 18, 2015 03:08
Swift-friendly NSCalendar methods
// NSCalendar+Swift.swift
// A set of Swift-idiomatic methods for NSCalendar
//
// (c) 2015 Nate Cook, licensed under the MIT license
extension NSCalendar {
/// Returns the hour, minute, second, and nanoseconds of a given date.
func getTimeFromDate(date: NSDate) -> (hour: Int, minute: Int, second: Int, nanosecond: Int) {
var (hour, minute, second, nanosecond) = (0, 0, 0, 0)
getHour(&hour, minute: &minute, second: &second, nanosecond: &nanosecond, fromDate: date)
@natecook1000
natecook1000 / TestEnum.swift
Created March 22, 2015 22:17
Enum case matching
enum Test {
case One
case Two
var testDot: String {
switch self {
case .One:
return "One"
case .Two:
return "Two"
@natecook1000
natecook1000 / easyAssociatedValues.swift
Created March 27, 2015 01:14
Easier access to an enum's associated values
enum Result {
case Success(String)
case Failure(String)
var successString: String? {
switch self {
case let .Success(str):
return str
default:
return nil
@natecook1000
natecook1000 / keybase.md
Created May 7, 2015 19:19
Keybase Proof

Keybase proof

I hereby claim:

  • I am natecook1000 on github.
  • I am nnnnnnnn (https://keybase.io/nnnnnnnn) on keybase.
  • I have a public key whose fingerprint is CAC7 670D 18D7 D73C AFDF 36C5 B26F D823 319D 4365

To claim this, I am signing this object:

protocol OptionalType {
typealias T
func flatMap<U>(@noescape f: (T) -> U?) -> U?
}
extension Optional : OptionalType { }
extension SequenceType where Generator.Element: OptionalType {
func flatten() -> [Generator.Element.T] {
return self.map { $0.flatMap { $0 } }