Skip to content

Instantly share code, notes, and snippets.

View cmoulton's full-sized avatar

Christina Moulton cmoulton

View GitHub Profile
@cmoulton
cmoulton / Todo+Networking.swift
Created November 16, 2016 16:02
Demo code for Strongly-Typed GET and POST Calls With Alamofire at https://grokswift.com/strongly-typed-api-calls/. Uses Swift 3.0 and Alamofire 4.0.
import Foundation
import Alamofire
enum BackendError: Error {
case objectSerialization(reason: String)
}
extension Todo {
class func endpointForID(_ id: Int) -> String {
return "https://jsonplaceholder.typicode.com/todos/\(id)"
@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)
}
//
// ViewController.swift
// starWarsTableViewUpdates
//
// Created by Christina Moulton on 2015-10-17.
// Copyright (c) 2015 Teak Mobile Inc. All rights reserved.
//
import UIKit
@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 / 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
@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()
}