【问题标题】:Save file but hide file extension - Cocoa with Key Value Coding保存文件但隐藏文件扩展名 - 带有键值编码的 Cocoa
【发布时间】:2016-07-19 08:55:00
【问题描述】:

我正在使用Key Value Coding 将一些对象保存到文件中。我希望隐藏已保存文件的扩展名(或至少隐藏,除非 Finder → 首选项 → 高级“显示所有文件扩展名”中的值为 true),但我似乎无法让它工作。

我正在像这样保存文件:

let saveDialog  = NSSavePanel()
        saveDialog.allowedFileTypes = ["purr"]

        saveDialog.beginWithCompletionHandler() { (result: Int) -> Void in
            if result == NSFileHandlingPanelOKButton {


                NSFileManager.defaultManager()
                    .createFileAtPath(saveDialog.URL!.path!, contents: NSData(), attributes: [NSFileExtensionHidden: NSNumber(bool: true)])
                let _ = NSKeyedArchiver.archiveRootObject(safePhrases, toFile: saveDialog.URL!.path!)
            }
        }

        return saveDialog.URL

但在 Finder 中查看保存的文件时,扩展名始终可见。我该如何解决这个问题?

【问题讨论】:

  • 你试过NSFileManagersetAttributes:ofItemAtPath:error:吗?
  • 成功了!你想写一个答案还是我应该?谢谢! do { try NSFileManager.defaultManager().setAttributes([NSFileExtensionHidden: NSNumber(bool: true)], ofItemAtPath: saveDialog.URL!.path!) } catch _{ Swift.print("Unable to hide extension") }
  • 我对 Swift 不熟悉,我觉得还是你做比较好。

标签: swift cocoa nsfilemanager key-value-coding nssavepanel


【解决方案1】:

根据 Willeke 的建议,我在写入文件后设置了属性,使用 NSFileManagersetAttributes:ofItemAtPath:error

do { try NSFileManager.defaultManager().setAttributes
    ([NSFileExtensionHidden: NSNumber(bool: true)], ofItemAtPath: saveDialog.URL!.path!) } 

catch _{ Swift.print("Unable to hide extension") }

【讨论】:

    猜你喜欢
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    相关资源
    最近更新 更多