在 Swift 3 中将文本附加到 NSTextView [英] Appending text to NSTextView in Swift 3

查看:29
本文介绍了在 Swift 3 中将文本附加到 NSTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序利用 MDM API 对设备的属性进行批量更新.

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

我希望在主视图中有某种可见的输出/日志记录,以便用户可以看到他们的 CSV 的哪些行可能失败以及 HTTP 响应代码是什么.我可以很容易地附加到 NSTextField 或标签上,但是看到可能有很多行的大输出,我想要一个可滚动的文本框来附加信息.

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.

我找到的所有用于执行此操作的文档似乎都是 Swift 2,而这是我遗漏的难题之一.

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.

我已经尝试将我的插座声明为 NSScrollViewNSTextView,但似乎两者都没有区别.insertText 似乎已被弃用.

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.

过去,在 Swift 2 上我使用过这个解决方案,它对我来说效果很好,但它没有不适用于 swift 3.

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.

推荐答案

我想我已经弄清楚了.我进入视图控制器场景并深入到 NSScrollView > Clip View > Text View,然后从中创建我的插座,而不是手动将其从 NSScrollView 更改为 NSTextView.

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

这篇关于在 Swift 3 中将文本附加到 NSTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆