Skip to content

Instantly share code, notes, and snippets.

Rob Ryan robertmryan

Block or report user

Report or block robertmryan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:2211d9947b4ad6109659
// example for http://stackoverflow.com/a/26261978/1271826
// create scroll view
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollViewSuperView addSubview:scrollView];
// create container 1
@robertmryan
robertmryan / gist:1ecf5c7f4bf4d78a8cbc
Last active Aug 29, 2015
An example of populating a HTTPBody of NSMutableURLRequest for `application/x-www-form-urlencoded` request in response to http://stackoverflow.com/questions/28008874/post-with-swift-and-api/28009796#comment44430194_28009796
View gist:1ecf5c7f4bf4d78a8cbc
extension NSMutableURLRequest {
/// Populate the HTTPBody of `application/x-www-form-urlencoded` request
///
/// :param: contentMap A dictionary of keys and values to be added to the request
func setBodyContent(contentMap: [String : String]) {
let parameters = map(contentMap) { (key, value) -> String in
return "\(key)=\(value.stringByAddingPercentEscapesForQueryValue()!)"
}
View AFNetworkingDemo.swift
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)
})
View gist:658b00944427e8601aff
// original class
class A {
var x: String
init(x: String) {
self.x = x
}
}
// new class
View gist:625b2e279a8f013c23e9
func myFunction() -> NSArray {
var array: NSArray
Synchronized(someObject) {
// Stuff to do.
// don't "return" the array, here, but just set that local var above
}
return array // you can only "return" the `NSArray` object from outside the `Synchronized` closure
}
View gist:2e2418e9bb9a3cb37181
var text: String!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSBundle.mainBundle().URLForResource("test", withExtension: "json")!
var files = [String : NSFileWrapper]()
var error: NSError?
files["file.json"] = NSFileWrapper(URL: url, options: nil, error: &error)
View gist:927aa2e1642a15844ba3
var thing = 0
func hi() {
// Do something
thing++
print(thing)
}
@IBAction func somethingHi(sender: AnyObject) {
hi()
@robertmryan
robertmryan / gist:c526b6a4e477428f2a15
Last active Feb 28, 2016
Lazily instantiate location manager
View gist:c526b6a4e477428f2a15
lazy var locationManager: CLLocationManager = {
let manager = CLLocationManager()
manager.requestWhenInUseAuthorization()
return manager
}()
View gist:06ef1a474d15267b4b45
@IBAction func calculateButton(sender: AnyObject) {
if let value = Int(number.text!) {
var isPrime = true
if value == 1 {
isPrime = false
}
for var i = 2; i < value; i++ {
if value % i == 0 {
@robertmryan
robertmryan / gist:680113628a06705e17dd
Last active Feb 28, 2016
Animate spaceship along path
View gist:680113628a06705e17dd
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)
You can’t perform that action at this time.