Skip to content

Instantly share code, notes, and snippets.

@robertmryan
robertmryan / gist:680113628a06705e17dd
Last active February 28, 2016 23:22
Animate spaceship along path
override func didMoveToView(view: SKView) {
let sprite = SKSpriteNode(imageNamed:"Spaceship")
sprite.xScale = 0.25
sprite.yScale = 0.25
addChild(sprite)
let origin = view.convertPoint(view.bounds.origin, toScene: self)
let lowerRight = view.convertPoint(CGPoint(x: CGRectGetMaxX(view.bounds), y:CGRectGetMaxY(view.bounds)), toScene: self)
let visibleFrame = CGRect(x: origin.x, y: origin.y, width: lowerRight.x - origin.x, height: lowerRight.y - origin.y)
let path = sinePathInRect(visibleFrame)
@robertmryan
robertmryan / AFNetworkingDemo.swift
Created January 15, 2016 09:32
AFNetworking 3.x example
let manager = AFHTTPSessionManager()
manager.GET("http://example.com/resources.json", parameters: nil, progress: nil, success: { (task: NSURLSessionDataTask, responseObject: AnyObject?) -> Void in
print("JSON: \(responseObject!)")
}, failure: { (task: NSURLSessionDataTask?, error: NSError) -> Void in
print("Error: %@", error)
})
@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")
}
}