Skip to content

Instantly share code, notes, and snippets.

View mtackes's full-sized avatar

Michael Tackes mtackes

View GitHub Profile
@mtackes
mtackes / NSURLSession-uploadTask-example.swift
Last active December 7, 2018 04:03
An basic example of using an upload task with a completion handler.
import Cocoa
let session = NSURLSession.sharedSession()
let request = NSURLRequest(URL: NSURL(string: "http://example.com")!)
let dataToUpload = "Some Arbitrary Data".dataUsingEncoding(NSUTF8StringEncoding)
let uploadTask = session.uploadTaskWithRequest(request, fromData: dataToUpload,
completionHandler: { (responseData, response, error) in
// Check on some response headers (if it's HTTP)
@mtackes
mtackes / NamedTupleSubscriptBug.swift
Last active August 29, 2015 14:22
Compiler error when subscripting classes with a tuple having a named parameter in the 0 position
struct GridPiece {}
// This compiles
class ReadOnlyGrid {
subscript(position: (x: Int, y: Int)) -> GridPiece {
return GridPiece()
}
}
// And this compiles
@mtackes
mtackes / Notes.swift
Last active August 29, 2015 14:10
Swift subscripting
// A simple data source containing basic string 'notes'
class Notes {
private var notesArray = [String]()
// Get & Set with some safety logic
subscript(index: Int) -> String {
// `note` arg is implicitly String (subscript return type)
set (note) {
if index < 0 {
notesArray.insert(note, atIndex: 0)
// Get the file manager
let fileManager = NSFileManager.defaultManager()
// Get the app's available documents directories
// For other options usable in iOS besides .DocumentsDirectory see:
// http://stackoverflow.com/questions/7268299/path-directory-usable-in-ios
let directoryURLs = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) as [NSURL]
// Cast to [NSURL] because it 'helpfully' gives back [AnyObject]
// iOS should only give you a single documents directory for your app