我正在构建一个应用程序,需要从文本字段中获取文本并将其输出到文件中。我在尝试将textStorage写入文件时遇到了问题,因为它必须是字符串。我收到的错误是“类型'NSTextStorage'的值没有成员'写'”
我尝试在新行上强制转换为字符串,但未能弄清楚。
if let textView = TextField.documentView as? NSTextView {
let result: NSTextStorage = textView.textStorage!
try! result.write(toFile: "/Desktop", atomically: false, encoding: String.Encoding.utf8)
}
答案 0 :(得分:0)
您可以通过成员j
访问NSTextStorage
的字符串内容。该值是string
,因此您需要将其转换为String
才能调用您正在调用的写函数。
NSString
您当然可以使用等效的let nsstring = result.string as NSString
try! nsstring.write(toFile: "/Desktop", atomically: false, encoding: String.Encoding.utf8)
String
。