【发布时间】:2016-05-25 02:37:50
【问题描述】:
有什么方法可以用来覆盖返回自定义类的方法吗?当我尝试使用自定义类作为返回类型覆盖任何方法时,Xcode 会抛出错误
以下是我的代码:
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()
}
}
错误是在覆盖这个方法时:
func methodWithCustomClassAsReturn() -> CustomClass
带有错误信息:
来自扩展的声明还不能被覆盖
【问题讨论】:
标签: ios swift inheritance overriding