Jeff Lockhart
07/21/2022, 7:37 PMinitWith...
functions (constructors) in an ObjC category in a .def file? I've had success declaring properties and functions that aren't in the public API of an ObjC class, but adding an initWith...
function doesn't add the constructor definition (or the @Deprecated initWith...
function that is usually generated with the constructor).
For example, where MyObjCClass
is previously declared in a public API header:
@interface MyObjCClass (Internal)
@property (nonatomic, readonly, nonnull) NSString *foo;
- (BOOL) isBar;
- (instancetype) initWithFoo: (NSString)foo bar: (BOOL)bar;
@end
Adds extension functions on the class definition, instead of modifying the class defined by the public API header:
expect val MyObjCClass.foo: NSInteger
@ObjCCallable external expect fun MyObjCClass.foo(): NSInteger
@ObjCCallable external expect fun MyObjCClass.isBar(): Boolean
There is nothing added for the initWithFoo:bar:
constructor though.Jeff Lockhart
07/22/2022, 3:06 AMJeff Lockhart
07/22/2022, 3:13 AMinitWith...
constructor declaration into the library's public API header for the class, immediately before the C interop task, so that it's added to the Kotlin class definition.Rick Clephas
07/22/2022, 8:35 AM@interface MyObjCClass ()
?Jeff Lockhart
07/22/2022, 3:48 PMRick Clephas
07/22/2022, 4:13 PMJeff Lockhart
07/22/2022, 4:37 PMstatic inline initMyObjCClass(NSString* foo, BOOL bar) {
return [[MyObjCClass alloc] initWithFoo:foo bar:bar];
}
And get the error:
error: no visible @interface for 'MyObjCClass' declares the selector 'initWithFoobar'
Jeff Lockhart
07/22/2022, 4:37 PMRick Clephas
07/22/2022, 4:40 PMRick Clephas
07/22/2022, 4:44 PM@interface MyObjCClass (Internal)
- (instancetype) initWithFoo: (NSString)foo bar: (BOOL)bar;
@end
MyObjCClass* MyObjCClass_initWithFoo(NSString* foo, BOOL bar) {
return [[MyObjCClass alloc] initWithFoo:foo bar:bar];
}
Then you could use MyObjCClass_initWithFoo
from Kotlin. It’s not as nice, but likely less work then modifying the original header.Jeff Lockhart
07/22/2022, 4:51 PMJeff Lockhart
07/22/2022, 4:53 PMJeff Lockhart
07/22/2022, 5:17 PMNSError*
parameter creates the error:
error: implicit conversion of an Objective-C pointer to 'NSError *__autoreleasing _Nullable * _Nullable' is disallowed with ARC