3

I am trying to attach few attributes to the NSAttributeString and keep getting the following error. Screenshot

 func strikeThroughStyle() {            
   let range = NSMakeRange(0, 1)
   // add styles to self
   self.attribute(self.string, atIndex: 0, effectiveRange: range)
 }

I get the error:

Cannot convert value of type 'NSRange' (aka '_NSRange') to expected argument type 'NSRangePointer' (aka 'UnsafeMutablePointer<_NSRange>')

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
JohnDoe
  • 75
  • 1
  • 10
  • this might duplicate with http://stackoverflow.com/questions/25825301/translate-nsrangepointer-from-objective-c-to-swift – Breek Feb 09 '16 at 19:39

1 Answers1

6

attribute:atIndex:effectiveRange: is a getter method — it doesn't set/attach/add attributes, it reports to you what the attribute value is at a particular index in the string. As such, the effectiveRange parameter is an "out-pointer": you pass in a pointer to an NSRange, and the method fills in data at that pointer when it returns. In Swift (and within an NSAttributedString extension) you can call it like this:

var range = NSRange()
let value = self.attribute(self.string, atIndex: 0, effectiveRange: &range)

However, that's not what you want. You seem to be wanting to set an attribute on the string, not get the value of an existing attribute. For that, use NSMutableAttributedString and the addAttribute:value:range: method, or (especially if you're applying attributes to an entire string) NSAttributedString's init(string:attributes:) constructor.

rickster
  • 124,678
  • 26
  • 272
  • 326