我可以在Swift框架库中使用Objective-C类吗? [英] Can I use an Objective-C class in my Swift framework library?

查看:75
本文介绍了我可以在Swift框架库中使用Objective-C类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个我在多个项目中使用的 Swift Framework 。有一个我想使用的Objectivce-C类很难直接插入Swift。让我们说类是SFTest

I have created a Swift Framework that I use in multiple projects. There is an Objectivce-C class that I would like to use in that is difficult to port in straight Swift. Lets say that class is SFTest

@interface SFTest {
} 
+ (NString *)myMethod(NSString *data);
@end
@implementation SFTest
+ (NString *)myMethod(NSString *data) {
    // does some processing
    return processedData;
}
@end

我可以以某种方式在我的中使用它Swift框架项目,如果有,怎么样?我对此的研究提到了一些叫做桥接头的东西,但它们不能在Swift框架中使用。

Can I somehow use this in my Swift framework project, and if yes, how? My research on this mentions something called bridging headers but that they can't be used in Swift frameworks.

推荐答案

所以这需要一个虽然我终于在我的设备上运行了一些东西。这是我的设置。我创建了一个名为swiftlib的Swift框架。我添加了一个名为MyClass的Objective-C类。基本上它看起来像这样:

So this took a while but I finally got something to run on my device. Here is my setup. I created a Swift Framework called swiftlib. I added an Objective-C class called "MyClass". Basically it looks like this:

然后我修改了导入我的新Objective-C类的伞形头。伞标题是swift库项目的名称。所以在我的情况下,它是swiftlib.h。这就是我的伞头文件中的内容:

I then modified the "umbrella header" to import my new Objective-C class. The umbrella header is the name of your swift library project. So in my case, it was "swiftlib.h". This is what is inside my umbrella header file:

//
//  swiftlib.h
//  swiftlib
//

#import <UIKit/UIKit.h>
#import "MyClass.h"

//! Project version number for swiftlib.
FOUNDATION_EXPORT double swiftlibVersionNumber;

//! Project version string for swiftlib.
FOUNDATION_EXPORT const unsigned char swiftlibVersionString[];

// In this header, you should import all the public headers of your     framework using statements like #import <swiftlib/PublicHeader.h>

确保将标题类设置为public,否则将出现编译错误。基本上,伞标题中包含的内容都暴露给您的swift框架的用户,因此它需要公开。选择MyClass.h并打开右侧详细信息栏并选择下拉列表:

Make sure to set your header class to public or you will get a compile error. Basically whatever is included in the umbrella header is exposed to users of your swift framework so it needs to be public. Select MyClass.h and open the right details bar and select the dropdown here:

然后我在一个视图应用程序中使用这个Swift框架进行测试,并且能够在AppDelegate.swift中使用我的Objective-C类,如下所示:

I then used this Swift framework in a single view application for testing and was able to use my Objective-C class in the AppDelegate.swift like so:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        let test = MyClass.myMethod()
        print("test = " + String(test))
        return true
    }

这在我的设备上编译并运行。

This compiled and ran on my device.

对于那些好奇的人来说,这是Objective-C MyClass的实现代码:

For those curious, this was the implementation code for the Objective-C MyClass:

#import "MyClass.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation MyClass

+ (NSString *)myMethod {
    NSString *key = @"test";
    NSString *data = @"mytestdata";
    const char *ckey = [key cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cdata = [data cStringUsingEncoding:NSASCIIStringEncoding];
    unsigned char chmac[CC_SHA256_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA256, ckey, strlen(ckey), cdata, strlen(cdata), chmac);
    NSData *hmac = [[NSData alloc] initWithBytes:chmac length:sizeof(chmac)];
    NSString *hash = [hmac base64Encoding];
    return hash;
}

@end

这篇关于我可以在Swift框架库中使用Objective-C类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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