在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

查看:100
本文介绍了在iOS应用程序项目中使用带有Swift代码的iOS Cocoa Static Library项目中的Objective-C代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的过程,将来自共享库项目的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:


  1. 启动Xcode 6.1.1

  2. 创建工作区测试

  3. 创建iOS Cocoa Touch静态库项目TestLibrary并将其添加到工作区

  4. 创建iOS单一视图应用程序项目测试(语言:Swift)并将其添加到工作区

  5. 添加 import TestLibrary to ViewController.swift

  1. start Xcode 6.1.1
  2. create workspace Test
  3. create iOS Cocoa Touch Static Library project TestLibrary and add it to workspace
  4. create iOS Single View Application project Test (language: Swift) and add it to workspace
  5. 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:


  1. 告诉TestLibrary应该导出TestLibrary.h。添加(大概)所需的语法和程序是什么桥接标题文件?

  2. 告诉测试TestLibrary所在的位置。由于应用程序和静态库项目都属于同一个工作区(并且位于文件系统中彼此相邻),我假设不需要明确的步骤,或者是否存在?

  1. Tell TestLibrary that is should "export" TestLibrary.h. What is the right syntax and procedure for adding the (presumably) required bridging header file?
  2. 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):


  1. 不要添加 import TestLibrary ,因为Swift和Objective-C代码都将驻留在同一个应用程序目标中

  2. 创建Test-Bridging-Header.h进行测试并添加# importTestLibrary / TestLibrary.h

  3. 设置构建设置 Objective-C Bridging Header:Test / Test-Bridging-Header.h for Test

  4. 从TestLibrary拖动libTestLibrary.a(在P下)使用二进制文件(在构建阶段)进入测试的链接库

  5. 添加 let test = TestLibrary()例如在ViewController.swift里面 viewDidLoad

  1. Do not add import TestLibrary, since both the Swift and Objective-C code will reside in the same app target
  2. Create Test-Bridging-Header.h to Test and add #import "TestLibrary/TestLibrary.h"
  3. Set build setting Objective-C Bridging Header: Test/Test-Bridging-Header.h for Test
  4. Drag libTestLibrary.a from TestLibrary (under Products) into Test's Link Library With Binaries (under Build Phases)
  5. Add let test = TestLibrary() e.g. inside viewDidLoadin ViewController.swift

Voila ......

Voila ...

这篇关于在iOS应用程序项目中使用带有Swift代码的iOS Cocoa Static Library项目中的Objective-C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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