Skip to content

Instantly share code, notes, and snippets.

@dirk
dirk / UIColor-hex.swift
Created October 22, 2015 06:35 — forked from blixt/HexToUIColor.swift
Swift UIColor extension that parses a hexadecimal color string
extension UIColor {
enum Error: ErrorType {
case Parsing(String)
}
convenience init(hex input: String) throws {
let hex = input.stringByTrimmingCharactersInSet(NSCharacterSet.alphanumericCharacterSet().invertedSet)
var int = UInt32()
guard NSScanner(string: hex).scanHexInt(&int) else {
throw Error.Parsing("Unable to scan hexadecimal integer")