Raja Raja - 1 month ago 8
Objective-C Question

Declare a string with char data type in Objective c .m file and use it in Swift

Objc file

#import "EncryptionConstants.h"

@implementation EncryptionConstants

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

@end


after creating a bridging file, I am doing this in a swift file..

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


Getting an error like:


value of EncryptionConstants has no member encKey.

Answer Source

Header file like Below :

#import <Foundation/Foundation.h>

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

@end

.m file is Below :

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

@implementation EncryptionConstants

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

@end

We have to call in swift as below :

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

Need to include

#import "EncryptionConstants.h"

in your bridging header