         var rangesForLinkType = [LinkRangeResult]()
 
         // Setup a regular expression for user handles and hashtags
-        var error: NSError?
-        let regex = NSRegularExpression(pattern: regexPattern, options: .CaseInsensitive, error: &error)
+        let regex: NSRegularExpression?
+        do {
+            regex = try NSRegularExpression(pattern: regexPattern, options: .CaseInsensitive)
+        } catch _ as NSError {
+            regex = nil
+        }
 
         // Run the expression and get matches
-        let length: Int = count(text)
+        let length: Int = text.characters.count
         if let matches = regex?.matchesInString(text, options: .ReportCompletion, range: NSMakeRange(0, length)) {
 
             // Add all our ranges to the result
             for match in matches {
-                if let textCheckingResult = match as? NSTextCheckingResult {
-                    let matchRange = textCheckingResult.range
-                    let stringIndexRange = advance(text.startIndex, matchRange.location)..<advance(text.startIndex, matchRange.location + matchRange.length)
-                    let matchString = text.substringWithRange(stringIndexRange)
-                    
-                    rangesForLinkType.append(LinkRangeResult(linkDetectionType: linkType, linkRange: stringIndexRange, linkString: matchString, textLink: nil))
-                }
+                let matchRange = match.range
+                let stringIndexRange = text.startIndex.advancedBy(matchRange.location)..<text.startIndex.advancedBy(matchRange.location + matchRange.length)
+                let matchString = text.substringWithRange(stringIndexRange)
+                
+                rangesForLinkType.append(LinkRangeResult(linkDetectionType: linkType, linkRange: stringIndexRange, linkString: matchString, textLink: nil))
             }
         }
 

        return attributes
    }
    
    private func attributesWithTextColor(textColor: UIColor) -> [NSObject: AnyObject] {
        var attributes = attributesFromProperties()
        attributes[NSForegroundColorAttributeName] = textColor
        


public class ContextLabel: UILabel, NSLayoutManagerDelegate {
    
    struct LinkDetectionType : OptionSetType {
        typealias RawValue = UInt
        private var value: UInt = 0
        init(_ value: UInt) { self.value = value }
        init(rawValue value: UInt) { self.value = value }
        init(nilLiteral: ()) { self.value = 0 }
        static var allZeros: LinkDetectionType { return self.init(0) }
        static func fromMask(raw: UInt) -> LinkDetectionType { return self.init(raw) }
        var rawValue: UInt { return self.value }
        
        static var None: LinkDetectionType { return self.init(0) }
        static var UserHandle: LinkDetectionType { return LinkDetectionType(1 << 0) }
        static var Hashtag: LinkDetectionType { return LinkDetectionType(1 << 1) }
        static var URL: LinkDetectionType { return LinkDetectionType(1 << 2) }
