Search code examples
swiftnsattributedstring

Strike through NSAttributedString: "Type of expression is ambiguous without more context"


Can anybody tell me what's wrong ?

let myTitle = NSAttributedString(string: Xdevices[row].deviceName!,
              attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 
              15.0)!,NSForegroundColorAttributeName:UIColor.orangeColor(), 
              NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle])

The error is:

Type of expression is ambiguous without more context

This happened after inserting NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle


Solution

  • Try this:

    let attributes = [
        NSFontAttributeName: UIFont(name: "Georgia", size: 15.0)!,
        NSForegroundColorAttributeName: UIColor.orangeColor(),
        NSStrikethroughStyleAttributeName: NSNumber(integer: NSUnderlineStyle.StyleSingle.rawValue)
    ]
    
    let myTitle = NSAttributedString(string: Xdevices[row].deviceName!, attributes: attributes)
    

    NSAttributedString(string:attributes:) expects a dictionary of type [String: AnyObject]. Howeverm StyleSingle is an Int. Hence you must wrap it inside an NSNumber.