Class conforming to protocol as function parameter in Swift

前端 未结 7 1251
萌比男神i
萌比男神i 2020-11-29 02:12

In Objective-C, it\'s possible to specify a class conforming to a protocol as a method parameter. For example, I could have a method that only allows a UIViewControlle

7条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 02:48

    Note in September 2015: This was an observation in the early days of Swift.

    It seems to be impossible. Apple has this annoyance in some of their APIs as well. Here is one example from a newly introduced class in iOS 8 (as of beta 5):

    UIInputViewController's textDocumentProxy property:

    Defined in Objective-C as follows:

    @property(nonatomic, readonly) NSObject *textDocumentProxy;
    

    and in Swift:

    var textDocumentProxy: NSObject! { get }
    

    Link to Apple' documentation: https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIInputViewController_Class/index.html#//apple_ref/occ/instp/UIInputViewController/textDocumentProxy

提交回复
热议问题