Skip to content

Instantly share code, notes, and snippets.

@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()
@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 / 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 / find4Largest.swift
Created January 19, 2015 03:21
Find 4 Largest
import UIKit
func find4Largest(numbers:[Int]) -> [Int]
{
let count = 4
assert(count < numbers.count, "Error: numbers must have at least \(count) elements")
if numbers.count < 2
{
return numbers
}