Skip to content

Instantly share code, notes, and snippets.

@robertmryan
robertmryan / gist:8af2c98902e39a60e177
Last active February 28, 2016 23:23
Resize views in `viewWillLayoutSubviews`
class ViewController: UIViewController {
var redView: UIView!
var greenView: UIView!
var blueView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
redView = UIView()
@robertmryan
robertmryan / gist:5ca3ef29330086e25675
Last active February 28, 2016 23:23
Range rendition of `splitEvery` in Swift 2
extension Range {
/// Split range into an array of ranges of predetermined size.
///
/// Adapted from http://stackoverflow.com/a/26691258/1271826
///
/// - parameter every: How many items per subrange
/// - returns: An array of Range objects
func splitEvery(every: Element.Distance) -> [Range] {
@robertmryan
robertmryan / mao.swift
Created March 1, 2016 20:03
Mao curve
override func viewDidLoad() {
super.viewDidLoad()
func sgn(x: Double) -> Double {
switch x {
case _ where x < 0:
return -1.0
case _ where x > 0:
return 1.0
default:
//: **Sort strings by numeric values only; ignoring non-numeric portions of strings**
import Cocoa
//: Persuant to [http://stackoverflow.com/a/35962434/1271826](http://stackoverflow.com/a/35962434/1271826), this illustrates a more concise way to accomplish what Mountain Man posted. This suffers from the same limitations as his code, though:
//:
//: - non-continguous numbers will be treated as single number (e.g. "He is 5 feet 10 inches tall" will be `510`);
//: - does not handle negative numbers; and
//: - does not handle floating point numbers
@robertmryan
robertmryan / dispatchTest.m
Last active March 26, 2016 16:21
dispatch_async works as expected
// this behaves as expected
- (void)exampleOne {
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:10];
NSLog(@"10s --- %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:5];
@interface Triangle : NSObject
@property (nonatomic) double sideALength;
@property (nonatomic) double sideBLength;
@property (nonatomic) double height;
@property (nonatomic, readonly) double area;
@end
@implementation Triangle
func subscript(index: Int) -> T {
guard index >= 0 && index < count else {
fatalError("Index out of range 0 ..< \(count)")
}
switch index {
case 0:
return head!.value
case count-1:
return tail!.value
func makeHTTPGetRequest(path: String, onCompletion: (JSON?, NSError?) -> ()) {
let request = NSMutableURLRequest(URL: NSURL(string: path)!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { data, response, error in
if let jsonData = data {
var parseError: NSError?
let json:JSON = JSON(data: jsonData, error: &parseError)
onCompletion(json, parseError)
} else {
onCompletion(nil, error)
@robertmryan
robertmryan / ViewController.swift
Last active July 4, 2016 19:58
Formatter validation in Swift for Cocoa
//
// ViewController.swift
//
// Created by Robert Ryan on 7/4/16.
// Copyright © 2016 Robert Ryan. All rights reserved.
//
import Cocoa
class TwoDigitFormatter: NumberFormatter {
class SingletonManager {
static let sharedObjectA = ObjectA()
}
class ObjectA {
func performAsyncTask(completionHandler: (String) -> ()) {
// perform something asynchronous which will call `completionHandler` later
completionHandler("foo")
}
}