Skip to content

Instantly share code, notes, and snippets.

@ajjames
ajjames / Bakery.swift
Created January 19, 2017 20:14
Bakery Example
import Foundation
class Bakery {
func bakeCake() -> Cake? {
let ingredientStorage = IngredientStorage()
if let chocolateFlavor = ingredientStorage.consume(flavor: .chocolate) {
return Cake(flavor: chocolateFlavor)
} else if let vanillaFlavor = ingredientStorage.consume(flavor: .vanilla) {
return Cake(flavor: vanillaFlavor)
}
@ajjames
ajjames / RawRepresentableConstants.swift
Last active August 28, 2024 14:09
Extendable 'enums' using RawRepresentable
import Foundation
/*
This is used by NSNotification.Name to allow enum-like members,
but where new members can be added in an extension!
enum Directions: String {
case north = "North"
}
@ajjames
ajjames / Registry.h
Created March 21, 2016 23:02
A simple Service Locator pattern for Swift and Obj-c
#import <Foundation/Foundation.h>
#define dep(p) [[Registry shared] resolve: (@protocol(p))]
#define reg(c, p) [[Registry shared] register:[c class] forProtocol:@protocol(p)]
@interface Registry : NSObject
@property (nonatomic, strong) NSMutableDictionary * _Nonnull classRegister;
@property (nonatomic, strong) NSMutableDictionary * _Nonnull classInstances;
+(instancetype _Nonnull)shared;
- (id _Nullable)resolve:(Protocol * _Null_unspecified)aProtocol;
@ajjames
ajjames / ImageViewerViewController.swift
Last active January 4, 2019 20:52
A simple drop-in single image viewer with async image loading
//
// ImageViewerViewController.swift
// PhotoViewer
//
// Created by Andrew James on 2/26/16.
// Copyright © 2016 aj. All rights reserved.
//
import UIKit
@ajjames
ajjames / StringExtension
Created November 30, 2015 17:59
String Extension for Formatting AttributedStrings
extension String
{
func attributeString(font1:UIFont, font2:UIFont, color1:UIColor = UIColor.blackColor(), color2:UIColor = UIColor.blackColor()) -> NSAttributedString
{
let attributes1 = [NSFontAttributeName:font1, NSForegroundColorAttributeName:color1]
let attributes2 = [NSFontAttributeName:font2, NSForegroundColorAttributeName:color2]
return attributeString("|", attributes1:attributes1, attributes2:attributes2)
}
func attributeString(delimiter:Character, attributes1:[String:AnyObject], attributes2:[String:AnyObject]) -> NSAttributedString
extension NSMutableAttributedString
{
func replace(target:String, with replacement:String, attributes:[String:NSObject]?)
{
var text = self.string
var error:NSError?
var regex = NSRegularExpression(pattern:target, options:.IgnoreMetacharacters, error:nil)
var range = NSMakeRange(0, count(text))
regex?.enumerateMatchesInString(text, options:nil, range: range, usingBlock: {
(result:NSTextCheckingResult!, flags:NSMatchingFlags, stop:UnsafeMutablePointer<ObjCBool>) -> Void in
extension NSAttributedString
{
convenience init?(html: String) {
if let data = html.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
{
let options = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
self.init(data: data, options: options, documentAttributes: nil, error: nil)
}
else
{
@ajjames
ajjames / singleton.swift
Created February 14, 2015 23:15
Swift Singleton
class mySingletonClass
{
class var default: mySingletonClass
{
struct Static
{
static var instance: mySingletonClass?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
@ajjames
ajjames / StringExtensions.swift
Created February 14, 2015 03:29
StringExtensions
import Foundation
public extension String
{
public func contains(substring:String) -> Bool
{
return self.rangeOfString(substring, options: NSStringCompareOptions.LiteralSearch, range: nil, locale: nil) != nil
}
}
@ajjames
ajjames / AsyncTestCase.swift
Last active August 29, 2015 14:14
AsyncTestCase: Simplifies asynchronous XCTest testing
import UIKit
import XCTest
let kAsyncTimeout = NSTimeInterval(30)
class AsyncTestCase: XCTestCase
{
var expectation: XCTestExpectation!
override func setUp()