Skip to content

Instantly share code, notes, and snippets.

@armstrongnate
armstrongnate / basic-auth.swift
Created July 20, 2014 21:45
HTTP Basic Authentication using NSURLSession in swift
import Foundation
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let userPasswordString = "username@gmail.com:password"
let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding)
let base64EncodedCredential = userPasswordData!.base64EncodedStringWithOptions(nil)
let authString = "Basic \(base64EncodedCredential)"
config.HTTPAdditionalHeaders = ["Authorization" : authString]
let session = NSURLSession(configuration: config)
@andygjp
andygjp / ReplaceNullContentWithNotFoundAttribute
Created October 15, 2014 21:58
A workaround that will recognise when a SingleResult type does not contain any results and replace it with a 404 instead of throwing a SerializationException.
internal class ReplaceNullContentWithNotFoundAttribute : EnableQueryAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
HttpResponseMessage httpResponseMessage = actionExecutedContext.Response;
if (httpResponseMessage.IsSuccessStatusCode && IsContentMissingValue(httpResponseMessage))
{
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.NotFound);
@gonzalezreal
gonzalezreal / ValueTransformer.swift
Last active February 8, 2017 16:22
Type safe NSValueTransformer
import Foundation
private class ValueTransformer: NSValueTransformer {
let transform: (AnyObject?) -> (AnyObject?)
init(transform: (AnyObject?) -> (AnyObject?)) {
self.transform = transform
}
// MARK: NSValueTransformer