Swift - calling C ++ files using Objective-C wrapper - passing Int values

I play with calls to C ++ methods from the Swift project. I used this tutorial to set and get a string value, worked fine.

Then I tried to do the same with an integer value, and I ran into some problems in my objective-c shell class.

#import <Foundation/Foundation.h>
#import "TestCppClassWrapper.h"
#include "TestCppClass.h"
@interface TestCppClassWrapper()
@property TestCppClass *cppItem;
@end
@implementation TestCppClassWrapper
- (instancetype)initWithTitle:(NSString*)title: (NSInteger*)variable
{
    if (self = [super init]) {
        self.cppItem = new TestCppClass(std::string([title cStringUsingEncoding:NSUTF8StringEncoding]), std::uintptr_t(variable));
    }
    return self;
}
- (NSString*)getTitle
{
    return [NSString stringWithUTF8String:self.cppItem->getTtile().c_str()];
}
- (void)setTitle:(NSString*)title
{
    self.cppItem->setTitle(std::string([title cStringUsingEncoding:NSUTF8StringEncoding]));
}
- (NSInteger*)getVariable
{
    return [NSInteger self.cppItem->getVariable()];
}
- (void)setVariable:(NSInteger*)variable
{
    self.cppItem->setVariable(std::NSInteger(variable));
}
@end

The problem arises here, as you may have guessed

enter image description here

I am not very familiar with either obj-c or C ++, so I can’t understand exactly how I should work with types, a string is an individual case (encoding, etc.), so it’s difficult to connect me with Inter.

#include "TestCppClass.h"
TestCppClass::TestCppClass() {}
TestCppClass::TestCppClass(const std::string &title, const std::int8_t &variable): m_title(title), m_variable(variable) {}
TestCppClass::~TestCppClass() {}
void TestCppClass::setTitle(const std::string &title)
{
    m_title = title;
}
void TestCppClass::setVariable(const std::int8_t &variable)

{
    m_variable = variable * 2;
}
const std::string &TestCppClass::getTtile()
{
    return m_title;
}
const std::int8_t &TestCppClass::getVariable()
{
    return m_variable;
}

Thanks in advance

+4
source share
2

NSInteger , typedef long, .

- (instancetype)initWithTitle:(NSString*)title: (NSInteger)variable
{
    if (self = [super init]) {
        self.cppItem = new TestCppClass(std::string([title cStringUsingEncoding:NSUTF8StringEncoding]), variable);
    }
    return self;
}

- (NSInteger) getVariable
{
    return self.cppItem->getVariable();
}
- (void)setVariable:(NSInteger)variable
{
    self.cppItem->setVariable(variable);
}

. ( , NSInteger Swift.)

std::int8_t - "plain".

+4

NSInteger(variable) - Objective C, . std. std::.

TestCppClass?

+1

Source: https://habr.com/ru/post/1650677/


All Articles