objective-c - 在 Objective c .m 文件中声明一个 char 数据类型的字符串,并在 Swift 中使用它

标签 objective-c swift swift3

目标文件

#import "EncryptionConstants.h"

@implementation EncryptionConstants

 char encKey[] = "secretKey";
 char iv[] = "secretIV";

@end

创建桥接文件后,我在一个 swift 文件中执行此操作..

var enc = EncryptionConstants()
            print(enc.encKey)

出现如下错误:

value of EncryptionConstants has no member encKey.

最佳答案

头文件如下:

#import <Foundation/Foundation.h>

@interface EncryptionConstants: NSObject
@property unsigned char* encKey;
@property unsigned char* iv;

@end

.m 文件如下:

#import <Foundation/Foundation.h>
#import "EncryptionConstants.h"

@implementation EncryptionConstants

- (instancetype)init
{
    self = [super init];
    if (self) {
        _encKey = "secretKey";
        _iv = "secretIV";
    }
    return self;
}

@end

我们必须像下面这样快速调用:

let enc = EncryptionConstants()
print(String(cString: enc.encKey))
print(String(cString: enc.iv))

需要包含

#import "EncryptionConstants.h"

在你的桥接头中

关于objective-c - 在 Objective c .m 文件中声明一个 char 数据类型的字符串,并在 Swift 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45728925/

相关文章:

swift - 在包含上使用 "!"

swift - 如何检查相对于 super View 的 View 状态

ios - Eureka 行在 Swift 3.0 中呈现 View Controller 和返回值

ios - 货币数字的格式

ios - swift 3 : Hide UITableView if Empty

ios - 如何在 UIViewController 和 UIView 之间进行协调

objective-c - 旋转和捏合(缩放)手势会扰乱父 UIView 及其 subview 框架

iphone - 列出 Google 云端硬盘中的所有文件夹内容

ios - 如何在 AsyncDisplayKit 中设置节点布局

objective-c - AVAudioPlayer 中断视频捕获