swift-extensions

in Tricks

Helpful Swift extensions

Well, I’ve been dipping my toes in Swift for the past couple of months and I have grown a few extensions that I would like to share: an extension for String class and UIColor class…

Wouldn’t be nice to be able to access String instances and get sections of it (substring) as a new String but with a simple syntax?

Wouldn’t defining UIColor instances with a String make life easier for developers when working with graphical designers?

Here you go guys two fantastic extensions. Put them anywhere in your code.

There’s a gist link: here

 

extension String
{
    subscript (i: Int) -> String {
        return String(Array(self)[i])
    }
 
    subscript (r: Range) -> String {
        var start = advance(startIndex, r.startIndex)
        var end = advance(startIndex, r.endIndex)
        return substringWithRange(Range(start: start, end: end))
    }
 
}
 
// access substring with normal array ranges like this
let someString = "123abcdef456"
println(someString[3..<9])
// outputs 'abcdef'
 
//to access a single caracter:
println(someString[1])
// outputs '2'

UIColor extension to accept String as input.

import UIKit
extension UIColor {
    convenience init(string:String) {
        var scolor = string.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
        scolor = scolor.stringByReplacingOccurrencesOfString("0x", withString: "", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
 
        switch(countElements(scolor)) {
            case 3:
                let red = scolor[0]
                let green = scolor[1]
                let blue = scolor[2]
                scolor = red+red + green+green + blue+blue + "ff"
            case 6:
                scolor = scolor + "ff"
            case 8:
                scolor = scolor + ""
                // so compiler doesn't complain
            default:
                scolor = "00000000"
 
        }
 
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0, a:CUnsignedInt = 0;
        NSScanner.scannerWithString(scolor[0..<2]).scanHexInt(&r)
        NSScanner.scannerWithString(scolor[2..<4]).scanHexInt(&g)
        NSScanner.scannerWithString(scolor[4..<6]).scanHexInt(&b)
        NSScanner.scannerWithString(scolor[6..<8]).scanHexInt(&a)
 
        var red:CGFloat = 0.0
        var green:CGFloat = 0.0
        var blue:CGFloat = 0.0
        var alpha:CGFloat = 0.0
 
        if let x = String(r).toInt() {
            red = CGFloat(x) / 255.0
        }
        if let x = String(g).toInt() {
            green = CGFloat(x) / 255.0
        }
        if let x = String(b).toInt() {
            blue = CGFloat(x) / 255.0
        }
        if let x = String(a).toInt() {
            alpha = CGFloat(x) / 255.0
        }
 
        self.init(red: red, green: green, blue: blue, alpha: alpha)
 
    }
}
 
//this extensions uses the String extension from above
let color = UIColor(string:"#ff0000")
// gives you a fully saturated red