https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
l

Luoqiaoyou

03/12/2020, 9:38 AM
i got error "Unable to call non-designated initializer as super constructor", when i try to inherit WKWebView in ios.
a

Artyom Degtyarev [JB]

03/12/2020, 1:20 PM
Seems like you already found an issue with explanations(https://github.com/JetBrains/kotlin-native/issues/2010). One can use only designated initializers when inheriting, for this class they are
initWithFrame:configuration
and
initWithCoder
. I’ve made this snippet to prove, that it should work correctly:
Copy code
import kotlinx.cinterop.*
import platform.WebKit.WKWebView
import platform.WebKit.WKWebViewConfiguration
import platform.Foundation.NSZeroRect

class myWebView : WKWebView(frame = NSZeroRect.readValue(), configuration = WKWebViewConfiguration())
l

Luoqiaoyou

03/12/2020, 2:01 PM
wow,it works. but how can i know which constructor is designated initializers?
a

Artyom Degtyarev [JB]

03/12/2020, 2:07 PM
That’s where the problem comes, in fact. One can check the frameworks headers contents manually, for example in the WKWebView.h there are such lines:
Copy code
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
the
NS_DESIGNATED_INITIALIZER
macro is what you need.
9 Views