Creating the custom storage class

So let's get going on the custom text storage class, declaring it to be a subclass of NSTextStorage, and adding two properties:

class CustomTextStorage: NSTextStorage {   let textContent = NSMutableAttributedString()   override var string: String {   return textContent.string   } } 

The textContent property is an NSMutableString, and as such we declare it with let. We won't change the instance to which the variable points, but we will change the instance's contents.

We then override the superclass's string property, to return the textContent's own string value.

Next we need to override a number of methods of the superclass, much of which is self-explanatory, with the possible exception of the somewhat clunky ...

Get Mastering macOS Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.