该语法是:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521
考虑:
protocol Meh { func doSomething(); } //Extend protocol Meh,where `Self` is of type `UIViewController` //func blah() will only exist for classes that inherit `UIViewController`. //In fact,this entire extension only exists for `UIViewController` subclasses. extension Meh where Self: UIViewController { func blah() { print("Blah"); } func foo() { print("Foo"); } } class Foo : UIViewController,Meh { //This compiles and since Foo is a `UIViewController` subclass,it has access to all of `Meh` extension functions and `Meh` itself. IE: `doSomething,blah,foo`. func doSomething() { print("Do Something"); } } class Obj : NSObject,Meh { //While this compiles,it won't have access to any of `Meh` extension functions. It only has access to `Meh.doSomething()`. func doSomething() { print("Do Something"); } }
let i = Obj(); i.blah();
但是下面的方法会奏效.
let j = Foo(); j.blah();
换句话说,Meh.blah()仅适用于UIViewController类型的类.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。