[swift-dev] [SourceKit] NSRange, Swift.String, and NSString

Jean-Pierre Simard jp at jpsim.com
Fri Mar 24 13:09:57 CDT 2017


I ended up writing some convenience APIs to perform these conversions along
with many other useful SourceKit<->Cocoa conversions like line+column,
UTF-8, UTF-16 and String.Index in SourceKitten. It's MIT-licensed so feel
free to grab the String extensions from the project yourself:
https://github.com/jpsim/SourceKitten/blob/master/Source/SourceKittenFramework/String+SourceKitten.swift

That being said, you might have an easier time working with SourceKitten
than with with SourceKit directly, since it does a whole lot more, like
dynamically resolving+loading which SourceKit to use, caching expensive
operations, easier multi-threaded access, generating documentation, etc.

On Fri, 24 Mar 2017 at 10:59 Tyler Stromberg via swift-dev <
swift-dev at swift.org> wrote:

> I'm currently working on integrating SourceKit with a macOS application.
> AppKit APIs (e.g. NSAttributedString, NSLayoutManager, etc) deal in terms
> of NSRange (UTF-16 code units?). SourceKit, however, deals in terms of
> integer offsets and lengths (UTF-8 code units?). Is there a more efficient
> or easier way to convert back and forth between the two other than doing
> the index(_:offsetBy:) -> samePosition(in:) dance?
> _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20170324/10ae0919/attachment.html>


More information about the swift-dev mailing list