Appending text to NSTextView in Swift 3

2019-04-14 03:33发布

问题:

I am building an application that utilizes an MDM API to make bulk updates to attributes of devices.

I am looking to have some sort of visible output/logging in the main view so that the user can see what lines of their CSV may have failed and what the HTTP Response Code was. I can append to an NSTextField or a label easy enough, but seeing as how there may be a large output with many lines, I'd like to have a scrollable text box to append the information to.

All of the documentation that I've found for doing this seems to be Swift 2, and this is the one piece of the puzzle I'm missing.

Alternatively, if there's a better way to display some sort of logging output on the main screen, I'd be open to that as well.

I've tried declaring my outlet as both an NSScrollView and an NSTextView, and neither seems to make a difference. insertText seems to be deprecated.

In the past, on Swift 2 I've used this solution and it worked well for me, but it doesn't work on swift 3.

回答1:

I think I got this figured out. I went into the view controller scene and drilled down into the NSScrollView > Clip View > Text View and then created my outlet out of that instead of just manually changing it from NSScrollView to NSTextView.

Once I did that, the following line did me just fine:

myTxtView.textStorage?.append(NSAttributedString(string: "Hello World"))