protocol-oriented

How to add protocol type as subview

旧巷老猫 提交于 2019-12-13 19:24:30
问题 So I wrote a simple protocol: protocol PopupMessageType{ var cancelButton: UIButton {get set} func cancel() } and have a customView: class XYZMessageView: UIView, PopupMessageType { ... } and then I currently have: class PopUpViewController: UIViewController { //code... var messageView : CCPopupMessageView! private func setupUI(){ view.addSubview(messageView) } } But what I want to do is: class PopUpViewController: UIViewController { //code... var messageView : PopupMessageType! private func

What's the reason to add functions to a protocol via an extension, why not just put it in the definition of the protocol itself?

前提是你 提交于 2019-12-11 06:18:59
问题 I've always wondered why when I see examples of protocols people tend to add most of the functions via an extension. Like this: protocol Flashable {}//Can be empty becuase function is in extension extension Flashable where Self: UIView //Makes this protocol work ONLY if object conforms to UIView (ie. uilable, uibutton, etc.) { func flash() { UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: { self.alpha = 1.0 //Object fades in }) { (animationComplete) in if

Why should not directly extend UIView or UIViewController?

时间秒杀一切 提交于 2019-11-28 11:40:50
I saw this question , with this code: protocol Flashable {} extension Flashable where Self: UIView { func flash() { UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: { self.alpha = 1.0 //Object fades in }) { (animationComplete) in if animationComplete == true { UIView.animate(withDuration: 0.3, delay: 2.0, options: .curveEaseOut, animations: { self.alpha = 0.0 //Object fades out }, completion: nil) } } } } And I wonder why do we you not just directly just extend UIView ? Or in similar cases extend UIViewController why twist it around with a where Self: Is it so

Swift - Protocol extensions - Property default values

会有一股神秘感。 提交于 2019-11-27 17:06:34
Let's say that I have the following protocol: protocol Identifiable { var id: Int {get} var name: String {get} } And that I have the following structs: struct A: Identifiable { var id: Int var name: String } struct B: Identifiable { var id: Int var name: String } As you can see, I had to 'conform' to the Identifiable protocol in struct A and struct B. But imagine if I had N more structs that needs to conform to this protocol... I don't want to 'copy/paste' the conformance (var id: Int, var name: String) So I create a protocol extension : extension Identifiable { var id: Int { return 0 } var

Swift - Protocol extensions - Property default values

流过昼夜 提交于 2019-11-26 22:31:10
问题 Let's say that I have the following protocol: protocol Identifiable { var id: Int {get} var name: String {get} } And that I have the following structs: struct A: Identifiable { var id: Int var name: String } struct B: Identifiable { var id: Int var name: String } As you can see, I had to 'conform' to the Identifiable protocol in struct A and struct B. But imagine if I had N more structs that needs to conform to this protocol... I don't want to 'copy/paste' the conformance (var id: Int, var