This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: **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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this behaves as expected | |
- (void)exampleOne { | |
dispatch_async(queue, ^{ | |
[NSThread sleepForTimeInterval:10]; | |
NSLog(@"10s --- %@", [NSThread currentThread]); | |
}); | |
dispatch_async(queue, ^{ | |
[NSThread sleepForTimeInterval:5]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface Triangle : NSObject | |
@property (nonatomic) double sideALength; | |
@property (nonatomic) double sideBLength; | |
@property (nonatomic) double height; | |
@property (nonatomic, readonly) double area; | |
@end | |
@implementation Triangle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// | |
// Created by Robert Ryan on 7/4/16. | |
// Copyright © 2016 Robert Ryan. All rights reserved. | |
// | |
import Cocoa | |
class TwoDigitFormatter: NumberFormatter { |