简体   繁体   English

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

[英]Can I use an Objective-C class in my Swift framework library?

I have created a Swift Framework that I use in multiple projects. 我创建了一个我在多个项目中使用的Swift框架 There is an Objectivce-C class that I would like to use in that is difficult to port in straight Swift. 有一个我想使用的Objectivce-C类很难直接移植到Swift中。 Lets say that class is SFTest 让我们说这个课程是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? 我可以以某种方式在我的Swift框架项目中使用它,如果是的话,怎么样? My research on this mentions something called bridging headers but that they can't be used in Swift frameworks. 我对此的研究提到了一些叫做桥接头的东西,但它们不能用在Swift框架中。

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. 我创建了一个名为swiftlib的Swift框架。 I added an Objective-C class called "MyClass". 我添加了一个名为“MyClass”的Objective-C类。 Basically it looks like this: 基本上它看起来像这样:

在此输入图像描述

I then modified the "umbrella header" to import my new Objective-C class. 然后我修改了“伞头”以导入我的新Objective-C类。 The umbrella header is the name of your swift library project. 伞标题是swift库项目的名称。 So in my case, it was "swiftlib.h". 所以在我的情况下,它是“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>

Make sure to set your header class to public or you will get a compile error. 确保将标题类设置为public,否则将出现编译错误。 Basically whatever is included in the umbrella header is exposed to users of your swift framework so it needs to be public. 基本上,伞标题中包含的内容都暴露给您的swift框架的用户,因此它需要公开。 Select MyClass.h and open the right details bar and select the dropdown here: 选择MyClass.h并打开右侧详细信息栏并选择下拉列表:

在此输入图像描述

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.swift中使用我的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

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

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

相关问题 Swift框架-在Objective-C类中使用Swift类参考 - Swift framework - Use Swift class reference in Objective-C class 如何在Xcode Objective-C项目中的.mm类扩展中导入/使用快速静态库/框架 - How to import/use swift static library/framework in to .mm class extension in Xcode Objective-C Project Swift框架或Objective-c库 - Swift framework or Objective-c library 如何在我的 Swift 类中使用 Objective-c 方法 - How use Objective-c method in my Swift Class 我应该在混合的Objective-C / Swift项目中使用Realm Objective-C或Realm Swift吗? - Should I use Realm Objective-C or Realm Swift in my mixed Objective-C / Swift project? 在swift框架中使用objective-c - use objective-c in a swift framework 在 Swift 中使用 Objective-C 作为库 - Use Objective-C in Swift as a Library 不能在Objective-C中使用带有枚举的Swift类 - can not use Swift class with enum in Objective-C 为什么我不能在我的 objective-c 代码中使用我的 swift 文件? - Why can't I use my swift files in my objective-c code? 我需要在objective-c 中使用swift 类并且无法设置我的Emilos-Swift.h 桥接文件来解析objective-c 中的任何swift 类 - I need to use a swift class in objective-c and cannot setup my Emilos-Swift.h bridging file to resolve any swift classes in objective-c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM