2

I have a subclass of NSTextStorage and I'm trying to remove the foreground color of a paragraph the following way:

var paragraphRange = self.string.paragraphRangeForRange(
        advance(self.string.startIndex, theRange.location)..advance(self.string.startIndex, theRange.location + theRange.length))

self.removeAttribute(NSForegroundColorAttributeName, range: paragraphRange)

However, I get the following error Cannot invoke 'removeAttribute' with an argument list of type '(String, range: (Range<String.Index>))'

Help Please. I think TextKit on Swift is a mess. Some methods receive/return NSRange but String works with Range<String.Index> making it a hell to work with.

Marcus Rossel
  • 3,196
  • 1
  • 26
  • 41
OscarVGG
  • 2,632
  • 2
  • 27
  • 34

1 Answers1

3

The problem here is that the NSString returned by self.string is automatically bridged to a Swift String. A possible solution is to convert it back to NSString explicitly:

func removeColorForRange(theRange : NSRange) {
    let paragraphRange = (self.string as NSString).paragraphRangeForRange(theRange)
    self.removeAttribute(NSForegroundColorAttributeName, range: paragraphRange)
}

Note also that the range operator .. has been replaced by ..< in newer Swift versions (to avoid confusion with ... and to emphasize that the upper bound is not included).

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382