将 NSTextStorage 写入文件
Writing NSTextStorage to file
我正在构建一个应用程序,我需要从文本字段中获取文本并将其输出到文件中。我在尝试将 textStorage 写入文件时遇到的问题,因为它需要是一个字符串。我收到的错误是 "Value of type 'NSTextStorage' has no member 'write'"
我试过在新的一行转换为一个字符串,但没搞清楚。
if let textView = TextField.documentView as? NSTextView {
let result: NSTextStorage = textView.textStorage!
try! result.write(toFile: "/Desktop", atomically: false, encoding: String.Encoding.utf8)
}
您可以通过成员string
访问NSTextStorage
的字符串内容。该值是 String
因此您需要将其转换为 NSString
以调用您正在调用的写入函数。
let nsstring = result.string as NSString
try! nsstring.write(toFile: "/Desktop", atomically: false, encoding: String.Encoding.utf8)
当然,您可以使用 String
等效项 write<Target>(to target: inout Target)
。
我正在构建一个应用程序,我需要从文本字段中获取文本并将其输出到文件中。我在尝试将 textStorage 写入文件时遇到的问题,因为它需要是一个字符串。我收到的错误是 "Value of type 'NSTextStorage' has no member 'write'"
我试过在新的一行转换为一个字符串,但没搞清楚。
if let textView = TextField.documentView as? NSTextView {
let result: NSTextStorage = textView.textStorage!
try! result.write(toFile: "/Desktop", atomically: false, encoding: String.Encoding.utf8)
}
您可以通过成员string
访问NSTextStorage
的字符串内容。该值是 String
因此您需要将其转换为 NSString
以调用您正在调用的写入函数。
let nsstring = result.string as NSString
try! nsstring.write(toFile: "/Desktop", atomically: false, encoding: String.Encoding.utf8)
当然,您可以使用 String
等效项 write<Target>(to target: inout Target)
。