Thursday, October 29, 2026
HomeiOS DevelopmentThe right way to move and return an object to a C++...

The right way to move and return an object to a C++ operate known as from iOS Swift?

[ad_1]

In my iOS challenge, I wish to name a C++ operate from my Swift code that takes an object from my Swift code and returns one other object.

To date, following this tutorial and including just a little bit extra stuff, I managed to name a C++ operate that takes a easy parameter and returns a string:

  • I create the NativeLibWrapper.h file:
#import <Basis/Basis.h>

@interface NativeLibWrapper : NSObject

- (NSString *) sayHello: (bool) boolTest;

@finish

  • I create the NativeLibWrapper.mm file:
#import <Basis/Basis.h>

#import "NativeLibWrapper.h"
#import "NativeLib.hpp"

@implementation NativeLibWrapper

- (NSString *) sayHello: (bool) boolTest {
    NativeLib nativeLib;
    std::string retour = nativeLib.sayHello(boolTest);
    return [NSString stringWithCString: retour.c_str() encoding: NSUTF8StringEncoding];
}

@finish
  • I create the NativeLib.hpp file:

#ifndef NativeLib_hpp
#outline NativeLib_hpp

#embody <stdio.h>
#embody <string>

class NativeLib
{
public:
    std::string sayHello(bool boolTest);
};

#endif /* NativeLib_hpp */

  • I create the NativeLib.cpp file:
#embody "NativeLib.hpp"

std::string NativeLib::sayHello(bool boolTest)
{
    return "Whats up C++ : " + std::to_string(boolTest);
}
  • I add the next in Runner/Runner/Runner-Bridging-Header.h:
#import "NativeLibWrapper.h"
  • And eventually, I name the C++ operate in Swift:
NativeLibWrapper().sayHello(true)

However now, for instance that my C++ sayHello() operate takes this sort of object as a parameter (so I can entry it within the C++ operate):

class TestAPOJO {

    var value1 = ""
    var value2 = false
}

and returns this sort of object (generated from the C++ operate however then mapped right into a Swift object, or one thing like that):

class TestBPOJO {

    var value3 = 0.0
    var value4 = false
}

How can I obtain that?

Thanks.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments