swift Self

'Self' is the type of a protocol/class/struct/enum.And the 'self' is a instance of a class/struct/enum.As for your requirement,maybe you could write like this:

class ZKCodingModel: NSObject{
    
    func filterSelf() -> Self {
        let result = self.dynamicType.init()
        return result
    }
    
    required override init() {
        
    }
}

class ZKSubclass: ZKCodingModel {
}

let sub = ZKSubclass()
//I want to call superclass's method to return subclass' type (ZKSubclass)
let filterdSub = sub.filterSelf()
print(filterdSub)

相当于oc的instance。
posted @ 2018-05-15 19:26  zzfx  阅读(115)  评论(0编辑  收藏  举报