Skip to content

Instantly share code, notes, and snippets.

@ajjames
ajjames / DispatchUtility.swift
Last active August 29, 2015 14:13
Dispatch Utility for succinct ways of calling GCD
//
// DispatchUtility.swift
//
// Copyright (c) 2015 Andrew James. All rights reserved.
//
import Foundation
func GCDDispatchMain(closure:()->())
{
@ajjames
ajjames / Downloader.swift
Last active June 14, 2020 08:12
Cancelable UIImageView async download extension (with simple NSCache-backed ImageManager class, and cancelable Downloader class)
//
// Downloader.swift
// Copyright (c) 2015 Andrew James. All rights reserved.
//
import Foundation
import UIKit
public class Downloader : NSObject
{
@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()
@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 / 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) {
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
{
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
@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
@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 / 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;