在iOS应用程序项目中使用带有Swift代码的iOS Cocoa Static Library项目中的Objective-C代码 [英] Using Objective-C code in iOS Cocoa Static Library project with Swift code in iOS Application project
问题描述
我正在寻找一个简单的过程,将来自共享库项目的Objective-C代码与来自应用程序项目的Swift代码相结合,但到目前为止这个序列没有成功:
I am looking for a simple procedure for combining an Objective-C code from a shared library project with Swift code from an application project but have had no success so far with this sequence:
- 启动Xcode 6.1.1
- 创建工作区测试
- 创建iOS Cocoa Touch静态库项目TestLibrary并将其添加到工作区
- 创建iOS单一视图应用程序项目测试(语言:Swift)并将其添加到工作区
- 添加
import TestLibrary
to ViewController.swift
- start Xcode 6.1.1
- create workspace Test
- create iOS Cocoa Touch Static Library project TestLibrary and add it to workspace
- create iOS Single View Application project Test (language: Swift) and add it to workspace
- add
import TestLibrary
to ViewController.swift
如果我现在构建Test,我在ViewController中收到此错误。 swift:没有这样的模块:'TestLibrary'
。
If I now build Test, I receive this error in ViewController.swift: No such module: ‘TestLibrary’
.
大概必须克服两个障碍:
Presumably two hurdles must be overcome:
- 告诉TestLibrary应该导出TestLibrary.h。添加(大概)所需的语法和程序是什么桥接标题文件?
- 告诉测试TestLibrary所在的位置。由于应用程序和静态库项目都属于同一个工作区(并且位于文件系统中彼此相邻),我假设不需要明确的步骤,或者是否存在?
- Tell TestLibrary that is should "export" TestLibrary.h. What is the right syntax and procedure for adding the (presumably) required bridging header file?
- Tell Test where TestLibrary is located. Since both the application and static library projects belong to the same workspace (and sit in the file system next to each other) I assume no explicit steps are required, or are there?
总而言之,我的问题是:即使我随后添加 let test = TestLibrary()
,我如何克服构建错误?到ViewController.swift,即Test(Swift代码库)如何利用TestLibrary(Objective-C代码库)?
So in summary, my question is this: how can I overcome the build error even if I subsequently add let test = TestLibrary()
to ViewController.swift, i.e. how can Test (Swift code base) make use of TestLibrary (Objective-C code base)?
推荐答案
此过程似乎有效(对于单个应用程序目标):
This procedure seems to work (for single app target):
- 不要添加
import TestLibrary
,因为Swift和Objective-C代码都将驻留在同一个应用程序目标中 - 创建Test-Bridging-Header.h进行测试并添加
# importTestLibrary / TestLibrary.h
- 设置构建设置
Objective-C Bridging Header:Test / Test-Bridging-Header.h
for Test - 从TestLibrary拖动libTestLibrary.a(在P下)使用二进制文件(在构建阶段)进入测试的链接库
- 添加
let test = TestLibrary()
例如在ViewController.swift里面viewDidLoad
- Do not add
import TestLibrary
, since both the Swift and Objective-C code will reside in the same app target - Create Test-Bridging-Header.h to Test and add
#import "TestLibrary/TestLibrary.h"
- Set build setting
Objective-C Bridging Header: Test/Test-Bridging-Header.h
for Test - Drag libTestLibrary.a from TestLibrary (under Products) into Test's Link Library With Binaries (under Build Phases)
- Add
let test = TestLibrary()
e.g. insideviewDidLoad
in ViewController.swift
Voila ......
Voila ...
这篇关于在iOS应用程序项目中使用带有Swift代码的iOS Cocoa Static Library项目中的Objective-C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!