I'm playing around with calling c++ methods from Swift project. I used this tutorial to set and get string value, worked perfectly.
Then I tried to do the same with integer value and I encountered some problems in my objective-c wrapper 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
Problem occurs here, as you may have guessed
I'm not much familiar neither with obj-c nor with c++, that's why I'm unable to figure how how exactly should I deal with types, string is quite individual case(encoding and stuff), so it's hard for me to relate it to Int.
#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