NSTextView & NSStringPboardType in Swift

When overriding readFromPasteboard and writeToPasteboard in an NSTextView, I need to check if the pasteboard's type is plain text.


NSTextView seems to be using NSPasteboard.PasteboardType(rawValue:"NSStringPboardType") as the plain text pasteboard type. The symbol NSStringPboardType is not available in Swift 4.2. Xcode 10 suggests NSPasteboard.PasteboardType.string instead, but it's not equal to NSStringPboardType, and has a raw value of 'public.utf8-plain-text'.


Is there a way to reference NSStringPboardType in Swift without using a hard coded string?

Replies

Do you actually need (or even want) to check for specific types? Can you use NSString's implementation of readableTypes(for:) — that it inherents from the NSPasteboardReading protocol — to see if NSString can read any of the types?

Thanks Ken, but it seems that NSString.readableTypes(for:) includes only public.utf8-plain-text as well, and not NSStringPboardType.

The point isn't to examine the list, it's to check if it's non-empty. If NSString can find something on the pasteboard it can use, then it's presumably plain text. You can then simply read that item. Have you actually tested if NSString says there's a readable type when there's only "NSStringPboardType"? (Also, in my tests, NSTextView puts both "NSStringPboardType" and "public.utf8-plain-text" on the pasteboard, with the same data.)