相关疑难解决方法(0)

Swift重写静态方法编译错误

我有这两个快速的课程:

class A {    
    static func list(completion: (_ result:[A]?) -> Void) {
        completion (nil)
    }        
    static func get(completion: (_ result:A?) -> Void) {
        completion (nil)
    }
}


class B: A {    
    static func list(completion: (_ result:[B]?) -> Void) {
        completion (nil)
    }    
    static func get(completion: (_ result:B?) -> Void) {
        completion (nil)
    }        
}
Run Code Online (Sandbox Code Playgroud)

尝试编译这会引发错误"覆盖声明需要'覆盖'关键字",但仅适用于B类的'get'方法.'list'方法编译良好.[B]有什么区别?和B?对于这种情况下的编译器?

编辑:另请注意,无法添加"覆盖".我收到错误'无法覆盖静态方法'.

static compiler-errors compilation swift

9
推荐指数
1
解决办法
7438
查看次数

覆盖子类Swift中的静态var

因式分解我的代码,我想设置从儿童类,我会在父类的类FUNC得到stringKey:

Chair.request();
Table.request();

class Furniture: NSObject {

    static let requestKey : String!

    class func request(){

        print("class key => \(self.requestKey)")
        ...
    }
}

class Chair: Furniture {
    static let requestKey : String = "jsonchairs"
} 

class Table: Furniture {
    static let requestKey : String = "tables"
} 
Run Code Online (Sandbox Code Playgroud)

当然,我有预编译的错误消息

属性不会覆盖其超类中的任何属性

有没有解决方案,或者我需要传递密钥作为参数?像这样:

Chair.request(key : "jsonchairs" );
Table.request(key : "tables" );
Run Code Online (Sandbox Code Playgroud)

static overriding var swift

5
推荐指数
2
解决办法
5383
查看次数

标签 统计

static ×2

swift ×2

compilation ×1

compiler-errors ×1

overriding ×1

var ×1