What is the standard process of subclassing NSTextLocation?

Cannot find any guidance in the forums and Developer Doc, the WWDC session Meet TextKit2 says this protocol is served for any location type espacially useful when the underlying doc model is not linear. But when I try to subclass the NSTextLocation with my own type to define a more structured location rather than a linear one, also with my own NSTextContentManager subclass implementation, I keep receiving the system internal Location model like NSCountableTextLocation compare to my location which cause the app to crash.

-[NSCountableTextLocation compare:] receiving unmatching type <MarkdownTK2.ChildIndexPathLocation: 0x9b2402aa0>

In my own NSTextContentManager subclass:

public override func offset(
from: any NSTextLocation,
to: any NSTextLocation
) -> Int {

this method will also both receive my own location and some times receiving the system defined location that I can not calculate the offset then just return zero.

The doc only says

If you provide your own implementation of the NSTextLocation protocol to manage locations in your content, subclass NSTextContentManager and implement your own storage object to support those locations.

  • OS
    • Development environment: Xcode 26.0, macOS 26.0
    • Run-time configuration: macOS 26.0
Answered by DTS Engineer in 862535022

Thanks for providing the demo project via the DTS channel. From there, I see that that you are using NSTextView to render the text. Currently, NSTextView & UITextView directly rely NSTextContentStorage. When using your own NSTextContentManager subclass with NSTextView, you'd get the NSTextContentStorage variant of the text locations. That explains what you have seen.

Implementing your own view to render the text should be able to fix the issue. That, however, will be quite involved. If you choose to go along this path, the following sample may give you a good starting point:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

This is likely related to the implementation of your NSTextContentManager subclass. Specifically, I am wondering if you return the right text locations of your own type in all the location-related methods, such as enumerateTextElements(from:options:using:) and location(_:offsetBy:). If you can provide a minimal project that demonstrates the issue, I may be able to take a look.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks for providing the demo project via the DTS channel. From there, I see that that you are using NSTextView to render the text. Currently, NSTextView & UITextView directly rely NSTextContentStorage. When using your own NSTextContentManager subclass with NSTextView, you'd get the NSTextContentStorage variant of the text locations. That explains what you have seen.

Implementing your own view to render the text should be able to fix the issue. That, however, will be quite involved. If you choose to go along this path, the following sample may give you a good starting point:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I'd say it depends. In the simple case I ever tried, I didn't need to customize NSTextLayoutManager. In the case where you have custom text elements and would customize the text selection behavior, you might need to. You might start with NSTextLayoutManager, and check in the forums if you see anything doesn't work.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

What is the standard process of subclassing NSTextLocation?
 
 
Q