Swift应用程序在Objective C库中定义的引用类型 [英] Reference types defined in Objective C Library from Swift Application

查看:52
本文介绍了Swift应用程序在Objective C库中定义的引用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常是一个使用Visual Studio的.NET开发人员,所以如果我误解了一些iOS术语,请原谅我.

I'm usually a .NET developer used to using Visual Studio so please forgive me if I get some of the iOS terminology wrong.

我想做的是引用在新的Swift iOS应用程序的Objective-C库中定义的类型.

What I am trying to do is reference a type defined in an Objective-C library in a new Swift iOS application.

具体来说,我正在尝试使用[本文的作者( https://danielsaidi.wordpress.com/2014/09/04/my-approach-to-ioc-in-ios/ ).

Specifically, I am trying to get dependency injection working using the CoreMeta library as described by the author of [this article(https://danielsaidi.wordpress.com/2014/09/04/my-approach-to-ioc-in-ios/).

在XCode中,我使用swift创建了一个新的空白单视图应用程序,并且添加了对本文中提到的CoreMeta库的引用(注意:关于添加一个我尝试将库拖放到swift应用程序上,然后将两个项目都添加到新的工作区中,但是这两种方法都导致了我将要描述的相同问题)

In XCode, I have created a new blank single view app using swift, and I have added a reference to the CoreMeta library mentioned in the article (Note: I'm a bit confused by what Xcode needs in terms of adding a reference. I tried dragging and dropping the library onto the swift app and adding both projects to a new workspace, but both methods lead to the same problem I am about to describe)

我知道我需要一个桥接头,因此我右键单击swift项目并添加一个新文件,选择头文件并以projectname-bridging-header.h格式添加一个新头.

I understand that I need a bridging header, so I right-click the swift project and add a new file, select header file and add a new header in the format projectname-bridging-header.h.

我阅读的文档建议Xcode应该为我创建此文件,但是我没有弹出此对话框.

The documentation I have read suggests that Xcode should offer to create this for me, But I have not had this dialog pop up.

在swift项目的构建设置中的常规选项卡下,我添加了libCoreMeta.a文件(我假设这是从objc库构建的程序集),并且在Swift编译器的Build Settings中-代码生成我的桥接头值指向我添加的文件.

In the build settings of the swift project, under the general tab, I have the libCoreMeta.a file added (i'm assuming this is the built assembly from the objc library) and in Build Settings under Swift Compiler - Code Generation I have the bridging header value pointing to the file I added.

这是我可能会感到困惑的地方.在桥接头文件中,我尝试执行的每个导入语句的每个变体都会导致找不到文件"错误,因此在应用程序的其余部分中不能使用任何类型.

This is where I may be getting confused. In the bridging header file every single variation of an import statement I try to do results in a "file not found" error and thus no types can be used in the rest of the app.

在本文中,IoCContainer对象是在NSObject + IoCContainer.h中定义的,因此#import"NSObject + IoCContainer.h"的任何变体似乎都无法正常工作.

In the article, the IoCContainer object is defined in NSObject+IoCContainer.h so any variation of #import "NSObject+IoCContainer.h" does not seem to be working.

我敢肯定,这是我很想念的东西,所以任何帮助都会很棒.

I'm sure this is something very obvious I am missing here, so any help would be great.

编辑

为简单起见,我刚刚创建了一个新的swift应用,向其中添加了CoreMeta库,添加了一个ObjectiveC文件(并获得了创建桥接头的请求),但是我仍然无法计算出要添加到桥接中的内容标头以访问CoreMeta库中的内容.下面的文件结构

For simplicity sake, I just created a new swift app, added the CoreMeta library to it, added an ObjectiveC file (and got the request to create a bridging header) but I still cant work out what to add to to the bridging header to access the stuff in the CoreMeta library. File structure below

和TestSwiftApp-Bridging-Header.h

and TestSwiftApp-Bridging-Header.h

很明显,我在名称空间和内容如何与iOS结合方面存在知识空白

I obviously have a knowledge gap in how namespaces and stuff works with iOS

推荐答案

Xcode 为您创建桥接头.单击File -> New -> File...,然后选择在Objective-C项目中创建Swift文件或在Swift项目中创建Objective-C文件时,都会执行以下操作:

Xcode will offer to create a bridging header for you. It does this when you click File -> New -> File... and then choose either to create a Swift file in an Objective-C project or an Objective-C file in a Swift project:

重要的不是桥接头名称的格式.桥接头可以具有您选择的任何名称.重要的是在构建设置"中将哪个文件设置为桥接头.

What's important though isn't the format of the bridging header's name. A bridging header can have whatever name you choose. What's important is what file is set as the bridging header in your Build Settings.

首先,在您的项目中创建任何头文件.

First, create any header file in your project.

在您的Project Navigator中,点击目标:

现在在此处单击目标:

然后在此处选择构建设置":

And select "Build Settings" here:

一直向下滚动到名为"Swift Compiler-Code Generation"的部分:

Scroll all the way on down to the section called "Swift Compiler - Code Generation":

注意:仅当您有要编译的.swift文件时,此部分才会真正显示.如果您的项目没有正在编译的.swift文件,那么将已经很混乱的构建设置"选项与一整套甚至不包括在您的项目中的语言的选项混为一谈就没有意义了.

As a note: this section will only actually show up if you have .swift files to compile. If you don't have any .swift files that your project is compiling, there is no point in cluttering the already cluttered Build Settings options with a whole set of options for a language you're not even including in your project.

如果Xcode从未为您提供自动配置桥接头的选项,则"Objective-C桥接头"行将为空白,而未选择文件.您所要做的就是单击此处并输入新文件名:

If Xcode never gave you the option to automatically configure a bridging header, the "Objective-C Bridging Header" line is either going to be blank with no file selected. All you have to do is click there and type your new file name:

有趣的事情要注意...如果需要,您可以定义不同的Debug和Release构建桥接头.

Interesting thing to note... you can define different Debug and Release build bridging headers if that's necessary.

关于将库的头文件导入到要用作桥接头的文件中的麻烦的最后说明:有必要实际获取要使用的库的头文件的副本使用.您不能只将.a文件放入您的项目中.您需要该库随附的.a和任何/所有对应的.h文件.因此,请确保您的项目中已有.h文件以及桥接头的正确设置.

As a final note about the trouble you're having importing the library's header file into the file you want to use as the bridging header: it is necessary that you actually obtain a copy of the header file for the library you're using. You can't just put the .a file in your project. You need the .a and any/all corresponding .h files that go with that library. So make sure you've got the .h files in your project along with your bridging header properly set up.

这篇关于Swift应用程序在Objective C库中定义的引用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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