问题描述
我有NSTextView,我可以将文本作为nsattributedstring。我可以使用NSSavePanel将文本另存为.txt文件,而不是格式化文本。
@IBAction func saveDNA(sender:AnyObject)
{
let saveDNAtoFile:NSSavePanel = NSSavePanel()
saveDNAtoFile.canSelectHiddenExtension = true
saveDNAtoFile.runModal()
do
{
让exportedFileURL = saveDNAtoFile.URL
让textDNA = self .inputDnaFromUser.string
,如果exportedFileURL!= nil
{
试试textDNA!.writeToURL(exportedFileURL !,原子性:false,编码:NSUTF8StringEncoding)
}
}捕获
{
}
}
如何我使用NSSavePanel将attributedstring(文本)保存到文件中,以便以后可以打开该文件以在格式化文本之前完成所有工作吗?如果我可以为此使用NSSavePanel,那么我应该在上面的代码中进行哪些更改?
一天出来...好吧,我已经找到了Swift 2的代码(请注意-选项:NSFileWrapperWritingOptions.Atomic)。下面。与这种标准功能相比,我相信它将为像我这样的初学者节省更多的时间,使他们有更多的时间编写必要的算法和更有趣的算法。
> @IBAction func saveDNA(sender:AnyObject){
让saveDNAtoFile:NSSavePanel = NSSavePanel()
saveDNAtoFile.canSelectHiddenExtension = true
saveDNAtoFile.runModal()
do
{
让exportedFileURL = saveDNAtoFile.URL
让textDNA = inputDnaFromUser.textStorage
如果exportedFileURL!= nil
{
let range = NSRange(0 ..< textDNA!.length)
let textTSave =试试textDNA!.fileWrapperFromRange(range,documentAttributes:[NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType])
尝试textTSave.writeToURL(exportedFileURL !,选项:NSFileWrapperWritingOptions.Atomic,originalContentsURL:nil)
}
}捕获
{
}
}
I have NSTextView and I can have text as nsattributedstring. I can save text into .txt file using NSSavePanel, as plain text, but not as formatted text.
@IBAction func saveDNA(sender: AnyObject)
{
let saveDNAtoFile: NSSavePanel = NSSavePanel()
saveDNAtoFile.canSelectHiddenExtension = true
saveDNAtoFile.runModal()
do
{
let exportedFileURL = saveDNAtoFile.URL
let textDNA = self.inputDnaFromUser.string
if exportedFileURL != nil
{
try textDNA!.writeToURL(exportedFileURL!, atomically: false, encoding: NSUTF8StringEncoding)
}
} catch
{
}
}
How can I save the attributedstring (text) into file using NSSavePanel, to be able later to open this file to have all made before formatting in the text? What I should change in the code above, if I can use NSSavePanel for this ?
One day out ... Ok, I have figured out the code for Swift 2 (note this - options: NSFileWrapperWritingOptions.Atomic). Below. I am sure it will save time for beginners like me, more time to write necessary and more interesting algorithms, than this standard functionality.
@IBAction func saveDNA(sender: AnyObject)
{
let saveDNAtoFile: NSSavePanel = NSSavePanel()
saveDNAtoFile.canSelectHiddenExtension = true
saveDNAtoFile.runModal()
do
{
let exportedFileURL = saveDNAtoFile.URL
let textDNA = inputDnaFromUser.textStorage
if exportedFileURL != nil
{
let range = NSRange(0..<textDNA!.length)
let textTSave = try textDNA!.fileWrapperFromRange(range, documentAttributes: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType])
try textTSave.writeToURL(exportedFileURL!, options: NSFileWrapperWritingOptions.Atomic, originalContentsURL: nil)
}
} catch
{
}
}
这篇关于如何将属性字符串(文本)保存到文件(SWIFT,可可)中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!