我可以在我的Swift框架库中使用Objective-C类吗?

时间:2022-09-07 09:15:58

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

我创建了一个在多个项目中使用的Swift框架。有一个objective - c类,我想在很难直接快速移植的情况下使用。假设类是SFTest

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

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.

我可以在我的Swift框架项目中使用这个吗?如果可以,如何使用?我的研究中提到了一种叫做桥接头的东西,但是它们不能在Swift框架中使用。

2 个解决方案

#1


16  

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:

这花了我一段时间,但我终于在我的设备上运行了一些东西。这是我的设置。我创建了一个名为swiftlib的Swift框架。我添加了一个名为“MyClass”的Objective-C类。基本上是这样的:

我可以在我的Swift框架库中使用Objective-C类吗?

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:

然后我修改了“伞头”以导入新的Objective-C类。伞头是您的swift库项目的名称。在我的例子中,它是swiftlib。h。这就是我的伞头文件:

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

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:

确保将header类设置为public,否则会出现编译错误。基本上,伞头中包含的内容都是向您的swift框架的用户公开的,所以它需要是公共的。选择MyClass。h打开正确的细节栏,在这里选择下拉:

我可以在我的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:

然后,我在一个视图应用程序中使用了这个Swift框架进行测试,并能够在AppDelegate中使用我的Objective-C类。斯威夫特一样:

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.

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

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

对于那些好奇的人来说,这是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

#2


-5  

First use objective c class in your swift project you have to create bridging header file in that file you have to import objective c class and then you can use that class in swift

首先在您的swift项目中使用objective - c类,您必须在该文件中创建桥接头文件,您必须导入objective - c类,然后您可以在swift中使用该类

#1


16  

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:

这花了我一段时间,但我终于在我的设备上运行了一些东西。这是我的设置。我创建了一个名为swiftlib的Swift框架。我添加了一个名为“MyClass”的Objective-C类。基本上是这样的:

我可以在我的Swift框架库中使用Objective-C类吗?

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:

然后我修改了“伞头”以导入新的Objective-C类。伞头是您的swift库项目的名称。在我的例子中,它是swiftlib。h。这就是我的伞头文件:

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

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:

确保将header类设置为public,否则会出现编译错误。基本上,伞头中包含的内容都是向您的swift框架的用户公开的,所以它需要是公共的。选择MyClass。h打开正确的细节栏,在这里选择下拉:

我可以在我的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:

然后,我在一个视图应用程序中使用了这个Swift框架进行测试,并能够在AppDelegate中使用我的Objective-C类。斯威夫特一样:

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.

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

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

对于那些好奇的人来说,这是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

#2


-5  

First use objective c class in your swift project you have to create bridging header file in that file you have to import objective c class and then you can use that class in swift

首先在您的swift项目中使用objective - c类,您必须在该文件中创建桥接头文件,您必须导入objective - c类,然后您可以在swift中使用该类