简体   繁体   English

Cocoa Touch Objective-C框架中的Swift类

[英]Swift class in Cocoa Touch Objective-C Framework

I'm trying to build a Cocoa Touch Framwork (XCode 6, obviously) with Objective-C and some Swift classes. 我正在尝试用Objective-C和一些Swift类构建一个Cocoa Touch Framwork(显然是XCode 6)。

I've successfully added Swift classes to regular projects (no framworks) before so I'm aware of the build settings required for this ( Embedded Content Contains Swift Code ). 我之前已经成功地将Swift类添加到常规项目(没有framworks),所以我知道这需要的构建设置( 嵌入式内容包含Swift代码 )。 Unfortunately, the framework project doesn't build the header file ( projectName-Swift.h ) and I'm not able to import the class and the Swift classes into any of my .m files. 不幸的是,框架项目没有构建头文件( projectName-Swift.h ),我无法将类和Swift类导入到我的任何.m文件中。

Does anybody know why this wouldn't work, and what build settings I need to change in order to make this work? 有谁知道为什么这不起作用,以及我需要改变哪些构建设置才能使其工作?

Finally found the answer; 终于找到了答案;

Within the frameworks the import statement for Swift files has to look as follows; 在框架内,Swift文件的import语句必须如下所示:

#import <ProjectName/ProjectName-Swift.h>

instead of 代替

#import "ProjectName-Swift.h"

Sometimes it helps to RTFM ( https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_82 ) 有时它对RTFM有帮助( https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_82

I've struggled with this for a while because I don't think the documentation is clear. 我已经挣扎了一段时间,因为我认为文档不清楚。 I have a slightly different project setup, and it's taken me a while to work this out so I thought I'd answer here. 我有一个稍微不同的项目设置,我需要一段时间来解决这个问题,所以我想我会在这里回答。

I have a project with name: MyProject In this project, I have a framework: MyFramwork 我有一个名为的项目:MyProject在这个项目中,我有一个框架:MyFramwork

I have a swift file in MyFramework which I want to import into an objective c file in MyFramework. 我在MyFramework中有一个swift文件,我想将其导入到MyFramework中的目标c文件中。

To do this, you need: 为此,您需要:

#import <MyFramework/MyFramework-Swift.h>

NOT

#import <MyProject/MyFramework-Swift.h>

Hope that helps someone. 希望能帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Single View Application(objective-c)以函数作为Cocoa Touch Framework(swift)的参数的调用方法 - Call method with function as argument of Cocoa Touch Framework (swift) from Single View Application (objective-c) 在Objective-C或Cocoa-Touch中是否有Tree类和Node类? - Is there a Tree class and Node class in Objective-C or Cocoa-Touch? 添加Swift文件后构建Swift Cocoa Touch Framework时出错: <unknown> :0:错误:无法构建Objective-C模块 - Error building Swift Cocoa Touch Framework after adding Swift file: <unknown>:0: error: could not build Objective-C module Objective-C /可可触摸中的变量 - Variables in Objective-C/Cocoa-touch 可可触摸的Objective-C中的表存储 - Table Storage in Objective-C with cocoa touch Swift框架-在Objective-C类中使用Swift类参考 - Swift framework - Use Swift class reference in Objective-C class Objective-C –在Cocoa-Touch中处理触摸事件的选项 - Objective-C – Options for handling touch events in Cocoa-Touch 如何在 Xcode 中使用 iOS\\Cocoa Touch\\Objective-C 类模板创建文件 - How do I create a file with the iOS\Cocoa Touch\Objective-C class template in Xcode 无法在框架项目的 Objective-C 代码中导入 Swift 类 - Unable to import Swift class in Objective-C code in a framework project iOS中涉及屏幕刮擦的Objective-C和可可触摸问题 - Objective-C & Cocoa Touch Issue involving Screen Scrapes in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM