Appending text to NSTextView in Swift 3

左心房为你撑大大i 提交于 2019-12-22 00:09:02

问题


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"))


来源:https://stackoverflow.com/questions/40478728/appending-text-to-nstextview-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!