Skip to content

Instantly share code, notes, and snippets.

View cmoulton's full-sized avatar

Christina Moulton cmoulton

View GitHub Profile
@cmoulton
cmoulton / UISwiftRestDemo
Last active September 9, 2021 21:01
Quick & dirty REST API calls with Swift 2.2. See http://grokswift.com/simple-rest-with-swift/
// MARK: Using NSURLSession
// Get first todo item
let todoEndpoint: String = "http://jsonplaceholder.typicode.com/todos/1"
guard let url = NSURL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = NSURLRequest(URL: url)
@cmoulton
cmoulton / ResponseJSONObjectSerializable.swift
Last active December 4, 2018 04:52
Demo code for Strongly-Typed GET and POST Calls With Alamofire at https://grokswift.com/strongly-typed-api-calls/. Uses Swift 2.0, SwiftyJSON 2.3.0, and Alamofire 3.0. (For Swift 3, see https://gist.github.com/cmoulton/733356fd0a29411153155606c0cdc2ba)
public protocol ResponseJSONObjectSerializable {
init?(json: SwiftyJSON.JSON)
}
@cmoulton
cmoulton / ViewController.swift
Created September 17, 2015 17:59
Start a counter at 10 and decrement when tapped (no lower than 0). Remembers value between runs of the app. https://www.reddit.com/r/swift/comments/3lbk2v/stuck_with_saving_a_number_and_decreasing_it_with/
class ViewController: UIViewController {
let clicksKey = "clicksKey"
let maxClicks = 10
@IBOutlet weak var clicksLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
updateLabel()
}
@cmoulton
cmoulton / Headers.Swift
Created September 27, 2015 14:28
Add & Remove Session Headers in Alamofire in Swift 2.0
func addSessionHeader(key: String, value: String) {
var headers:[NSObject : AnyObject]
if let existingHeaders =
alamofireManager.session.configuration.HTTPAdditionalHeaders as? [String: String] {
headers = existingHeaders
} else {
headers = Manager.defaultHTTPHeaders
}
headers[key] = value
let config = alamofireManager.session.configuration
//
// ViewController.swift
// starWarsTableViewUpdates
//
// Created by Christina Moulton on 2015-10-17.
// Copyright (c) 2015 Teak Mobile Inc. All rights reserved.
//
import UIKit
//
// ViewController.swift
// starWarsTableViewUpdates
//
// Created by Christina Moulton on 2015-10-17.
// Copyright (c) 2015 Teak Mobile Inc. All rights reserved.
//
import UIKit
import UIKit
class ScrollDemoViewController: UITableViewController {
var objects = [AnyObject]()
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
@cmoulton
cmoulton / DetailViewController.swift
Last active February 21, 2020 04:20
Using DZNEmptyDataSet in a UIViewController
//
// DetailViewController.swift
// objcInterop
//
// Created by Christina Moulton on 2015-07-02.
// Copyright (c) 2015 Teak Mobile Inc. All rights reserved.
//
import UIKit
import DZNEmptyDataSet
@cmoulton
cmoulton / HeaderAuth.swift
Created June 29, 2016 16:45
HTTP Header Auth with Alamofire
func doMashape() -> Void {
let manager = Alamofire.Manager.sharedInstance
let headers = ["X-Mashape-Key": "MY_API_KEY", "Accept": "application/json"]
manager.request(.GET, "https://mashape-community-urban-dictionary.p.mashape.com/define?term=hipster", headers: headers)
.responseString { _, _, result in
if let receivedString = result.value {
print(receivedString)
}
}
}
@cmoulton
cmoulton / BasicAuth.swift
Created June 29, 2016 16:46
Alamofire Basic Auth
func doGetWithBasicAuthCredential() -> Void {
let username = "myUsername"
let password = "myPassword"
let credential = NSURLCredential(user: username, password: password, persistence: NSURLCredentialPersistence.ForSession)
Alamofire.request(.GET, "https://httpbin.org/basic-auth/\(username)/\(password)")
.authenticate(usingCredential: credential)
.responseString { _, _, result in
if let receivedString = result.value