如何向基于文档的 Swift 3 应用程序添加简单的保存加载功能?

Posted

技术标签:

【中文标题】如何向基于文档的 Swift 3 应用程序添加简单的保存加载功能?【英文标题】:How to add simple save-load functions to document-based Swift 3 app? 【发布时间】:2017-02-13 09:01:36 【问题描述】:

我目前正在研究 Swift(只是出于爱好),并试图弄清楚我应该如何在基于文档的 swift 应用程序中将函数保存和加载到 document.swift 文件中?我想知道如何保存和加载简单的 txt 文件。我正在使用 NSTextView,所以我想我必须将其更改为 NSString?

目前这些功能如下:

override func data(ofType typeName: String) throws -> Data 
        // Insert code here to write your document to data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning nil.
        // You can also choose to override fileWrapperOfType:error:, writeToURL:ofType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
        throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)      
    

override func read(from data: Data, ofType typeName: String) throws 
        // Insert code here to read your document from the given data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning false.
        // You can also choose to override readFromFileWrapper:ofType:error: or readFromURL:ofType:error: instead.
        // If you override either of these, you should also override -isEntireFileLoaded to return false if the contents are lazily loaded.
        throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)

    

【问题讨论】:

【参考方案1】:

在您的 document.swift 文件中,以下这些函数将加载并设置您的文本视图字符串值。

希望这会有所帮助!

var content = ""
override func makeWindowControllers() 
    //Make the window controller
    let storyboard = NSStoryboard(name: "Main", bundle: nil)
    let windowController = storyboard.instantiateController(withIdentifier: "Document Window Controller") as! NSWindowController
    self.addWindowController(windowController)

    //Access the view controller
    let vc = windowController.contentViewController as! ViewController
    //Set the text view string to the variable that was loaded
    vc.textView.string = content


override func data(ofType typeName: String) throws -> Data 
    //Access the current view controller
    if let vc = self.windowControllers[0].contentViewController as? ViewController 
        //Get the textView's String Value; or call a function that will create a string of what needs to be saved
        return vc.textView.string?.data(using: String.Encoding.utf8) ?? Data()
    else 
        throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
    


override func read(from url: URL, ofType typeName: String) throws 
    do 
        //Set a string variable to the loaded string
        //We can't directly set the text views string value because this function is called before the makeWindowControllers() so now text view is created yet.
        content = try String(contentsOf: url, encoding: String.Encoding.utf8)
    catch 
        throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
    

【讨论】:

谢谢!如果我有任何问题,我会参与其中并报告。

以上是关于如何向基于文档的 Swift 3 应用程序添加简单的保存加载功能?的主要内容,如果未能解决你的问题,请参考以下文章

如何在重置之前向日志添加值(CoreData 和 Swift)

如何通过 swift 3 的 snapkit 向任何 UI 添加动画?

如何在 ios 中将数据类型声明为时间戳 - swift

如何在 Swift 中使用色调和一些文本向 UIImageView 添加叠加层?

如何在 Swift 中向响应式网站添加后退按钮

使用 Swift 的基于文档的应用程序