Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
983 views
in Technique[技术] by (71.8m points)

xcode - Reference types defined in Objective C Library from Swift Application

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

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

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/).

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)

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.

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

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.

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.

Edit

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

swift app file structure

and TestSwiftApp-Bridging-Header.h

header file errors

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here


You can create a bridging header without going through these steps however.

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. enter image description here

In your Project Navigator, click the target:

enter image description here

Now click the target here:

enter image description here

And select "Build Settings" here:

enter image description here

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

enter image description here

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.

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:

enter image description here

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


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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...