1

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.

1

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

It just fixes the compilation error but the value is not being set properly. warning on the line _encKey = "secretKey"; Assigning to 'unsigned char *' from 'char [10]' converts between pointers to integer types with different sign
We can change the char type to NSString
It is a technical requirement to have the data type as char
I have updated the answer. print(String(cString: enc.encKey)) print(String(cString: enc.iv)) Please check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.