繁体   English   中英

如何在XCode 7中为Mac OS应用程序关联现有文件类型?

[英]How to associate existing file type for Mac OS application in XCode 7?

我正在为Xcode 7中的jpeg图片制作一个简单的查看器,并尝试将Jpeg文件类型与我的应用程序关联。 这是Swift中用于OS X的Cocoa应用程序,它使用情节提要,而不是基于文档的应用程序。

我在网上找到的教程建议浏览“信息”选项卡,并在其中添加新的文档类型。 现在,情况开始看起来与教程中的有所不同:在其中,仅需要填写3个字段(名称,类型,图标),但是我还有更多内容。 我尝试进行试验,这是我输入的字段的内容:

  • 名称:JPEG图像
  • 类: 留空
  • 扩展名:.jpg,.jpeg,.JPEG,.JPG
  • 图标: 留空
  • 标识符:public.jpeg
  • 角色:查看者
  • 哑剧类型:图片/ jpeg
  • 默认情况下会部分检查“文档作为分发包分发”; 我只是离开了它。
  • 也没有触摸其他文档类型属性。

结果,当我与其他已安装的应用程序一起单击Jpeg文件时,我的应用程序显示在“打开方式”的列表中,但是一旦尝试以这种方式打开它,就会出现一个弹出窗口,显示“文档”图片。 jpg”无法打开。 MyApp无法打开“ JPEG图像”格式的文件

我究竟做错了什么?

“类别”字段是必填项,您必须具有相应的实现。

尝试:

Class: $(PRODUCT_MODULE_NAME).Document

然后添加Document.swift

import Cocoa

class Document : NSDocument
{

    override init() {
        super.init()
        // Add your subclass-specific initialization here.
    }

    override class func autosavesInPlace() -> Bool {
        return true
    }

    override func makeWindowControllers() {
        // Returns the Storyboard that contains your Document window.
        let storyboard = NSStoryboard(name: "Main", bundle: nil)
        let windowController = storyboard.instantiateController(withIdentifier: "Document Window Controller") as! NSWindowController
        self.addWindowController(windowController)
    }

    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)
    }

}

^根据需要进行修改。

另外,您指定了角色“查看器”,这意味着您可以使用空格键将其打开,而不是双击(打开)-“编辑”角色对吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM