[swift-evolution] Specified Protocol Conformances in Subclasses

Rod Brown rodney.brown6 at icloud.com
Fri Mar 10 06:08:22 CST 2017


Hi everyone. I found something odd that seems baked into how Cocoa Touch does protocols, but Swift cannot model it:


@interface UIScrollView: UIView

@property (weak, nonatomic) id <UIScrollViewDelegate> delegate;

@end

@interface UITableView: UIScrollView

@property (weak, nonatomic) id <UITableViewDelegate> delegate;

@end

@protocol UITableViewDelegate: UIScrollViewDelegate
...
@end



Subclasses can further specify the conformance of a property’s protocol-conforming object to state a further type on that property. I tried to do something extremely similar today in Swift, and it failed, saying the protocols were different:


class SourceBar: UIScrollView {
	
	override var delegate: SourceBarDelegate? {
		get { return super.delegate as? SourceBarDelegate }
		set { super.delegate = newValue }
	}

} 


@objc protocol SourceBarDelegate: UIScrollViewDelegate {
	func foo()
}


Considering the fact that the protocol SourceBarDelegate conforms to UIScrollViewDelegate, I can’t see why this override should fail. Can anyone enlighten me as to why this is a limitation in the language?

Thanks,

Rod


More information about the swift-evolution mailing list