I'm currently using `initBy(ViewController(aDecode...
# kotlin-native
s
I'm currently using
initBy(ViewController(aDecoder))
, but
initBy
is deprecated. The message tells me I can replace
initBy
with
@OverrideInit
on the constructor, but every time I do, I get an error:
constructor with @OverrideInit doesn't override any super class constructor.
Is there an example somewhere of how to use this annotation properly? It seems no matter what I do it doesn't work.
s
To “override” a constructor, your constructor should have the same parameter names and types. Maybe you have wrong parameter names? For example, in your code from previous thread you have
constructor(aDecoder: NSCoder)
, and
aDecoder
parameter name doesn’t match super constructor.
s
Here is what I'm doing:
Copy code
@ExportObjCClass
class ViewController @OverrideInit constructor(aDecoder: NSCoder) : UIViewController(aDecoder) {
    @ObjCOutlet lateinit var label: UILabel
I still see the error when compiling
Error:(7, 1) constructor with @OverrideInit doesn't override any super class constructor.
s
Your constructor should have the same parameter names and types as super constructor, which is
Copy code
constructor(coder: NSCoder)
So your constructor parameter should be
coder: NSCoder
too:
Copy code
class ViewController @OverrideInit constructor(coder: NSCoder) : UIViewController(coder) {
s
Ah, that works! Thanks 🙂