将Swift与OS X首选项窗格插件一起使用 [英] Using Swift with an OS X Preference Pane plugin

查看:133
本文介绍了将Swift与OS X首选项窗格插件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Swift为系统偏好设置"应用构建OS X首选项窗格插件,但是我无法使其正常工作.

I'd like to use Swift to build an OS X Preference Pane plugin for the System Preferences app, but I can't get it to work.

单击下一步"后,Xcode模板没有提供选择Swift作为语言的选项,而是自动在Objective-C中创建项目.

After clicking "Next" the Xcode template doesn't offer an option to choose Swift as a language, but automatically creates the project in Objective-C.

不添加任何代码 或执行任何其他操作,即可成功构建项目.如果右键单击产品并选择在外部编辑器中打开",则系统偏好设置"将成功安装并加载偏好设置窗格.

Without adding any code or doing anything else, the project builds successfully. If you right-click on the Product and select "Open in External Editor", System Preferences will successfully install and load the preference pane.

它可以正常工作!

It just works!

很好,但是现在,我想使用Swift添加一个新的Cocoa子类.

Well that's great, but now, I want to add a new Cocoa subclass using Swift.

接受默认值,并允许它创建桥接头.

Accepting the default, and allowing it to create the Bridging Header.

现在,退出系统偏好设置,并 不添加任何代码 ,重新生成项目.与以前一样,右键单击产品",然后单击在外部编辑器中打开".

Now, quit System Preferences and without adding any code, rebuild the project. As before, right-click the Product and "Open in External Editor".

系统偏好设置将确认替换了偏好面板,然后将其安装,但无法加载.

System Preferences will confirm replacing the preference pane, and it will install it, but then it fails to load.

如果您在Finder中显示内置产品,则除了.prefPane插件外,还有一个.swiftmodule文件夹.

If you show the built Product in the Finder, in addition to the .prefPane plugin, there's also a .swiftmodule folder.

我猜测在构建阶段"或构建设置"中缺少一些内容,这些内容负责将.swiftmodule与其余捆绑软件合并在一起,但未能弄清楚.

I'm guessing there's something missing in the Build Phases or Build Settings that's responsible for incorporating the .swiftmodule with the rest of the bundle, but haven't been able to figure it out.

添加一些使用新类的代码后,有必要导入 Swift项目的伞形标头("Prax-Swift.h")进行项目编译,但是导入伞形标头不能解决此问题.

After you add some code that uses the new class, it's necessary to import the Swift project umbrella header ("Prax-Swift.h") to make the project compile, but importing the umbrella header doesn't fix this problem.

//  Prax.h

#import <PreferencePanes/PreferencePanes.h>
#import "Prax-Swift.h"

@interface Prax : NSPreferencePane

@property PraxObject *ourPrax;

- (void)mainViewDidLoad;

@end

我还尝试删除Prax.h和Prax.m,仅在Swift中实现NSPreferencePane子类.和以前一样,该项目可以生成和安装,但是系统偏好设置"无法加载它.

I also tried deleting Prax.h and Prax.m and simply implementing the NSPreferencePane subclass in Swift. As before, the project builds and installs, but System Preferences fails to load it.

//  Prax.swift

import PreferencePanes

class Prax: NSPreferencePane {

    override func mainViewDidLoad() {

    }
}

很抱歉,如果我在这个问题中使用了太多图片,这似乎是解释问题并使其易于重现的最清晰方法.可能有一个简单的解决方案.有什么想法吗?

Sorry if I've used too many pictures in this question; it seemed to be the clearest way to explain the problem and make it easy to reproduce. There's probably a simple solution. Any ideas?

推荐答案

首先,您需要启用嵌入式内容包含Swift"设置,以便Xcode将必要的Swift库复制到分发包中.

First, you need to enable the "Embedded Content Contains Swift" setting so that Xcode will copy the necessary Swift libraries into the bundle.

然后,您会收到此错误:

Then, you get this error:


System Preferences[68872]: dlopen_preflight failed with
  dlopen_preflight(/.../preftest.prefPane/Contents/MacOS/preftest):

  Library not loaded: @rpath/libswiftAppKit.dylib
    Referenced from: /.../preftest.prefPane/Contents/MacOS/preftest  
    Reason: image not found for /.../preftest.prefPane

这意味着该应用程序不知道从何处加载包含的Swift库.

This means the app doesn't know where to load the included Swift libraries from.

要解决此问题,请在构建设置的运行路径搜索路径中添加@loader_path/../Frameworks,以告知Swift库位于您的前面板的Frameworks目录中:

To fix this, add @loader_path/../Frameworks to the runpath search paths in the build settings, telling it that the Swift libraries are in the Frameworks directory of your prefpane:

请参见

See the dyld man page for further info about dynamic loading.

这篇关于将Swift与OS X首选项窗格插件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆