如何对文件->Save/File->在 Swift 应用中打开进行 link 操作

How to link actions to File->Save/File->Open in Swift app

我有一个 Swift 应用程序,其中包含我要使用 中描述的方法保存的数据。现在,我需要知道将这些操作 link 到文件 -> Save/Save 作为菜单项和文件 -> 打开菜单项的正确方法是什么。这不是基于文档的应用程序。

我 运行 Xcode 6.4 OS X 10.10.4.

创建一个 IBAction 函数并 link 通过 Interface Builder 将其发送到 XIB。

在该函数中创建一个 open/save 面板,让用户 select 文件名和位置,使用返回的 NSURL 数组作为 saving/loading 路径。 (当然是在转换为所需的对象类型之后。)

几乎到处都有很多示例代码,Objective-C 或 Swift。

在 Swift 3 中,它可能看起来很奇怪,因为它使用 'First Responder' 但您所要做的就是将以下代码添加到设置为 Custom 的 NSViewController class Class 在故事板上。它不必像其他 @IBAction 函数那样连接。

class Test: NSViewController {

@IBAction func saveDocument(_ sender: Any?) {
// code to execute for save functionality
// following line prints in debug to show function is executing.
// delete print line below when testing is completed.
Print("save")  
}

@IBAction func openDocument(_ sender: Any?) {
// code to execute for open functionality here
// following line prints in debug to show function is executing.
// delete print line below when testing is completed.
 print("open")
}

}