I have a kotlin function returning a Nullable like the following:
fun getBoolOrNil(): Boolean? {
if (something){
return true
}
return null
}
I then crosscompile this with kotlin native to objectiveC for iOs, but the method in the resulting Library that I use with objC/swift returns
__attribute__((swift_name("KotlinBoolean")))
@interface MyLibBoolean : MyLibNumber
- (instancetype)initWithBool:(BOOL)value;
+ (instancetype)numberWithBool:(BOOL)value;
@end;
whilst a MyLibNumber is just a
@interface MyLibNumber : NSNumber
...
Is this the crosscompilers try to give me something I can use like an Optional/Nullable or is this Object unusable for that purpose? I know that ObjectiveC does not support Optionals/Nullables, but I do not understand this Object that the Crosscompiler gives me here.
The closest your function translates to Objective C is something like:
- (NSNumber * _Nullable)getBoolOrNil {
if(something) {
return [NSNumber numberWithBool:YES];
}
return NULL;
}
The usage of NSNumber
with initWithBool
is a common pattern of expressing the nullable boolean in Objective-C.
So that's what your cross-compiler did, except it also created a wrapper for NSNumber
, and now the same function at the top, could be expressed as:
- (MyLibBoolean * _Nullable)getBoolOrNil {
if(something) {
return [MyLibBoolean numberWithBool:YES];
}
return NULL;
}
Of course in Swift, this whole wrapping is not needed, the function would look almost exactly the same as in kotlin:
func getBoolOrNil() -> Bool? {
if something {
return true
}
return nil
}