When using gobind via gomobile on iOS for an interface type, a golang function returning an error results in 2 impacts to the class in Objective C (example below):
- The inclusion of a NSError pointer passed in
- The method returns a boolean
I can infer how to use the NSError pointer, it's a standard objective C practice. But what value should I be returning for the boolean? TRUE on error, FALSE on success? The opposite? Something else? I can't seem to find documentation anywhere.
Example
An interface like this:
type A interface {
DoThing(data *DataType) error
}
Get's an objective C interface like this:
@interface PackageA : NSObject <goSeqRefInterface, PackageA> {
}
@property(strong, readonly) _Nonnull id _ref;
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
// Important bit is here:
- (BOOL)doThing:(data* _Nullable)DataType error:(NSError* _Nullable* _Nullable)error;
@end
In Objective-C, it is standard for methods that perform actions which might result in an error to return a boolean indicating success or failure, with
YESfor success andNOfor failure, and to accept anNSError **parameter to provide error details when necessary.Applying this to the code generated by
gomobileandgobind, you should handle the boolean return value in the same way.For your Go interface:
gomobilewill generate an Objective-C interface like (as you mention):An Objective-C method would then be:
In this pattern,
gomobilefollows the same conventions as Apple's Objective-C methods, which is to return a boolean value indicating the success of the operation, with an optionalNSErrorto detail any errors that occurred.