Group Group Group Group Group Group Group Group Group Forums

Text Kit Tutorial: Getting Started

Learn how to easily layout your text in iOS 8 using Swift in this Text Kit tutorial!

This is a companion discussion topic for the original entry at

I’m getting this error in the “applyStylesToRange” function of the “SyntaxHighlightTextStorage” file. At the line of code reading “self.addAttributes(attributes, range: matchRange)”, this error occurs:

Cannot convert value of type ‘[NSObject : AnyObject]’ to expected argument type ‘[String : AnyObject]’

Please advise how to fix this.

This is how I fixed it (Swift 3):

func applyStylesToRange(searchRange: NSRange) {
    let normalAttrs = [NSFontAttributeName : UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)]
    // iterate over each replacement
    for (pattern, attributes) in replacements {
        let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options(rawValue: 0))
        regex.enumerateMatches(in: backingStore.string, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: searchRange) {
            match, flags, stop in
            // apply the style
            let matchRange = match?.rangeAt(1)
            self.addAttributes(attributes as! PropertyList, range: matchRange!)
            // reset the style to the original
            let maxRange = (matchRange?.location)! + (matchRange?.length)!
            if maxRange + 1 < self.length {
                self.addAttributes(normalAttrs, range: NSMakeRange(maxRange, 1))

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]