9

In the documentation of the enumerateAttribute method it is said, regarding the stop argument of the block, that:

The block can set the value to true to stop further processing of the set.

However, inside the block the stop argument is a let and I can't set it to true.

I need to stop enumerating after the first attribute occurrence found. How could I do that?

AppsDev
  • 12,319
  • 23
  • 93
  • 186
  • Closely related: http://stackoverflow.com/questions/24214136/how-to-stop-enumerateobjectsusingblock-swift. – Martin R Jan 09 '17 at 12:09

1 Answers1

22

The parameter is a reference that holds the actual value:

let attributed: NSAttributedString = ...

attributed.enumerateAttribute(
    NSFontAttributeName,
    in: NSRange(location: 0, length: attributed.length),
    options: []
) { value, range, stop in
    stop.pointee = true
}

See the reference for UnsafeMutablePointer.

Sulthan
  • 128,090
  • 22
  • 218
  • 270