Skip to content

Instantly share code, notes, and snippets.

Avatar

Christina Moulton cmoulton

View GitHub Profile
@cmoulton
cmoulton / UISwiftRestDemo
Last active Sep 9, 2021
Quick & dirty REST API calls with Swift 2.2. See http://grokswift.com/simple-rest-with-swift/
View UISwiftRestDemo
// 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 Dec 4, 2018
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)
View ResponseJSONObjectSerializable.swift
public protocol ResponseJSONObjectSerializable {
init?(json: SwiftyJSON.JSON)
}
@cmoulton
cmoulton / ViewController.swift
Created Sep 17, 2015
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/
View ViewController.swift
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 Sep 27, 2015
Add & Remove Session Headers in Alamofire in Swift 2.0
View Headers.Swift
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
View ViewController.swift
//
// ViewController.swift
// starWarsTableViewUpdates
//
// Created by Christina Moulton on 2015-10-17.
// Copyright (c) 2015 Teak Mobile Inc. All rights reserved.
//
import UIKit
View ViewController.swift
//
// ViewController.swift
// starWarsTableViewUpdates
//
// Created by Christina Moulton on 2015-10-17.
// Copyright (c) 2015 Teak Mobile Inc. All rights reserved.
//
import UIKit
View ScrollDemoViewController.swift
import UIKit
class ScrollDemoViewController: UITableViewController {
var objects = [AnyObject]()
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
@cmoulton
cmoulton / DetailViewController.swift
Last active Feb 21, 2020
Using DZNEmptyDataSet in a UIViewController
View DetailViewController.swift
//
// 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 Jun 29, 2016
HTTP Header Auth with Alamofire
View HeaderAuth.swift
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 Jun 29, 2016
Alamofire Basic Auth
View BasicAuth.swift
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