Consider a C++ method that retrieve struct of native typed arguments like enum class, sub-structs, std::string, int, etc...
I'd like to create a swift API that return the same struct but in swift variables
for example :
class ErrorMessage {
public:
int status;
std::string message;
};
class serverResponse {
public:
ErrorMessage error;
std::string str_value;
std::uint16_t int_val;
std::time_t last_seen;
EnumVal status;
};
serverResponse getServerResponse();
So I'd like to convert it to the swift equivalent struct with native members
open class serverResponseSwift : NSObject {
open class var error: ErrorMessage { get }
open var str_value: String { get }
open var int_val: UInt16 { get }
open var status: EnumVal { get }
};
I know that direct conversion is not yet possible so I need to use objective-C++ code as a mediator. So I've used a bridging header to include the converting method in objective-C++ which will look like this :
and the equivalent .mm file will implement the conversion function, but can I use the swift Class in objective-c in order to fill it up according to the CPP serverResponse ?
@implementation Converter
- (serverResponseSwift) getServerStatusSwift {
serverResponse x = getServerResponse();
/// How do I create serverResponseSwift out of serverResponse
}
From why I read... In swift it's better dealing with errors using exceptions, and not by passing the error reference variable. However, if I throw an exception in the objective-c or the cpp part (for example, in method - (ServerResponseSwift*)serverResponseSwift { , than it wouldn't be supported in the swift part..
So I thought about adding method to fetch the data from swift (besides @objc init) and use it every time I want to fetch some data... But perhaps there's a better way (I know that in init we cannot use exceptions) ?
getData() {
self.error = error
// here I'll throw an exception if the error is set
...
}
Swift Error values are not exceptions, and it's important to drop whatever conceptions of error handling you have with them right away when using Swift or Obj-C. Using an error parameter in Obj-C will be automatically translated into throwing function in Swift (makes with throws) as long as it's properly structured.
@Jon_Shier . Hi and thanks for the comment. regarding your statement
Using an error parameter in Obj-C will be automatically translated into throwing function in Swift (makes with throws) as long as it's properly structured.
Perhaps you can give me minimal example so I can understand how it works ? I assume that if the signature of the objc method include (NSError *) as last parameter, it's parsed by the swift context and converted into exception if error != nil. right ?
@tera, I followed you guidelines, and it works great ! thanks.
Perhaps you can just add some usage example of how to call this API from swift code.
For example what i did, but i don't know how to handle the error - I'd like to get an exception if the object from type serverResponseSwift has valid error field
As @Jon_Shier rightly mentioned swift error handling is not called exceptions: even if it uses similar try and catch keywords the mechanism is totally different. This shows a few examples: Handling Cocoa Errors in Swift.
Your obj-c serverResponseSwift needs to return BOOL and have NSError** as the last parameter, and in swift you'll call it with trytry? or try! as in the examples.
The thing is that I would rather return the swift object and not BOOL so that i can use it later on the code.
The NSError** part is hidden in swift so i don't mind adding it.
is there an option that I can write the objective-c signature with NSError** but returning the ServerResponseSwift object and still get the exception