I'm working on sending a bunch of data to and from a program written in c++ to a Swift 3 user interface. I'm trying to do this by just passing a long json string to a simple c function that takes a char string as an input and output.
Here is the only file I include in the bridging header:
#ifndef ModelInterface_h
#define ModelInterface_h
#ifdef __cplusplus
extern "C" {
#endif
const char* ModelInterface(const char *input);
#ifdef __cplusplus
}
#endif
#endif /* ModelInterface_h */
Here is how I call it in Swift 3.0
let input = "Send In"
let test = ModelInterface(input)
print("Test \(String(cString: test!)) <-- test")
Here is the code in the cpp file:
#include "json.hpp"
#include "ModelInterface.h"
#include "ShellSitzwohl.h"
// for convenience
using json = nlohmann::json;
const char* ModelInterface(const char *input) {
ShellSitzwohl shell = ShellSitzwohl();
ShellFullOutput output = shell.Stress(10, 10);
// conversion: person -> json
json j = output;
std::stringstream stringstream;
stringstream << j;
const std::string tmp = stringstream.str();
const char* cstr = tmp.c_str(); // I can see that cstr is populated correctly
// return "This works!"; // If I just return this then it gets displayed
// Test This works! <-- test
return cstr; // This runs but not displayed correctly
// Test <-- test
}
The json string appears to be created and if I pass back the static string "This works!" it gets displayed correctly. Not sure if something is being released that shouldn't before the string gets back to swift.