ios - Cannot override method with a custom class return type -
is there method can use override method returns custom class? when tried override method custom class return type, xcode throws me error
below codes:
class customclass { var name = "myname" } class parent: uiviewcontroller { override func viewdidload() { methodwithnoreturn() print(methodwithstringasreturn()) methodwithcustomclassasreturn() } } extension parent { func methodwithnoreturn() { } func methodwithstringasreturn() -> string { return "parent methodwithreturn" } func methodwithcustomclassasreturn() -> customclass { return customclass() } } class child: parent { override func methodwithnoreturn() { print("can override") } override func methodwithstringasreturn() -> string { print("cannot override") return "child methodwithreturn" } override func methodwithcustomclassasreturn() -> customclass { return customclass() } }
the error when overriding method:
func methodwithcustomclassasreturn() -> customclass
with error message:
declarations extensions cannot overridden yet
no reason other compiler doesn't support yet. override method defined in extension superclass, must declare objc-compatible:
extension parent { func methodwithnoreturn() { } func methodwithstringasreturn() -> string { return "parent methodwithreturn" } @objc func methodwithcustomclassasreturn() -> customclass { return customclass() } } // means must expose customclass objc making inherit nsobject class customclass: nsobject { ... }
the alternative, without involving objc snafus define these methods in parent
class (not inside extension):
class parent: uiviewcontroller { override func viewdidload() { methodwithnoreturn() print(methodwithstringasreturn()) methodwithcustomclassasreturn() } func methodwithnoreturn() { } func methodwithstringasreturn() -> string { return "parent methodwithreturn" } func methodwithcustomclassasreturn() -> customclass { return customclass() } }
Comments
Post a Comment