简体   繁体   English

一个类中的多个委托 - Swift 5

[英]Multiple delegate in one class - Swift 5

Is it okay to have multiple delegate reference in one file.可以在一个文件中有多个委托引用吗? Maybe this is a dumb question, but I just want to know if theres pros or cons if I did this.也许这是一个愚蠢的问题,但我只想知道如果我这样做是利弊。 Thank you.谢谢你。

protocol SampleDelegate1: AnyObject {}
protocol SampleDelegate2: AnyObject {}

class Sample {
    weak var delegate1: SampleDelegate1?
    weak var delegate2: SampleDelegate2?
}

Yes, you can have multiple protocols, if needed.是的,如果需要,您可以拥有多个协议。 It might be illustrative to consider a few UIKit patterns:考虑一些 UIKit 模式可能是说明性的:

  • Table views and collection views have distinct protocols with different functional purposes, one for “data sources” and another for “delegates”.表视图和集合视图具有不同的协议,具有不同的功能用途,一个用于“数据源”,另一个用于“委托”。

  • URLSession employs a slightly different pattern, with a single delegate reference, a URLSessionDelegate , which has a hierarchy of different subprotocols to cover different functional needs (eg, URLSessionTaskDelegate inherits URLSessionDelegate , etc.). URLSession采用了稍微不同的模式,只有一个delegate引用,即URLSessionDelegate ,它具有不同子协议的层次结构来满足不同的功能需求(例如, URLSessionTaskDelegate继承URLSessionDelegate等)。

While different situations call for slightly different approaches, the idea is that a complicated interface can be broken down into different protocols (and optionally, either separate protocol references or single one) which cover distinct functional needs.虽然不同的情况需要稍微不同的方法,但其想法是,一个复杂的接口可以分解为不同的协议(并且可选地,单独的协议引用或单个协议引用),这些协议涵盖了不同的功能需求。

But you say:但是你说:

Actually, my only purpose is to pass data to multiple view controller.实际上,我唯一的目的是将数据传递给多个视图控制器。 I have a sign up function and when this is triggers, I am passing data to view controllers.我有一个注册功能,当它被触发时,我将数据传递给视图控制器。 my problem is I have 2 types of sign up, sign up for individual and sign up for corporate that is sharing a single sign up function.我的问题是我有两种类型的注册,个人注册和共享单一注册功能的公司注册。 I only want to pass data to the view controllers that are related to the sign up type.我只想将数据传递给与注册类型相关的视图控制器。 I thought creating two delegates will solve my problem, delegate just for individual sign up and corporate signup.我认为创建两个代表将解决我的问题,代表仅用于个人注册和公司注册。

It is really hard to be specific on the basis of so little information, but on the surface, this doesn't sound like a compelling use-case for multiple protocols, much less separate delegate references.在如此少的信息的基础上很难具体说明,但从表面上看,这听起来不像是多个协议的引人注目的用例,更不用说单独的委托引用了。 The difference here is not different functional domains, but rather slightly different payloads (a “user” vs “corporate” account type).这里的区别不是不同的功能域,而是稍微不同的有效负载(“用户”与“公司”帐户类型)。

I might advise a single protocol that passes back an “authentication” object which includes a “user type” or “permissions structure”, in which the caller determines “oh, on the basis of the returned authentication result I will transition to such-and-such view” or what have you.我可能会建议一个单一的协议,它传回一个“身份验证”对象,其中包括一个“用户类型”或“权限结构”,其中调用者确定“哦,根据返回的身份验证结果,我将转换到这样 - 并且-这样的观点”或者你有什么。 But to the extent the sign-up can stick with a single protocol/interface, keeping objects as loosely coupled as possible, the better, IMHO.但是在注册可以坚持使用单一协议/接口的范围内,尽可能保持对象松散耦合,恕我直言,更好。

Sure, why not, if it's useful for something you're doing?当然,如果它对你正在做的事情有用,为什么不呢? It's not that unusual.这不是那么不寻常。 A WKWebView has two delegates: WKWebView 有两个委托:

https://developer.apple.com/documentation/webkit/wkwebview https://developer.apple.com/documentation/webkit/wkwebview

However, if the goal is to notify multiple objects of the same thing, that sounds more like an NSNotification architecture or similar.但是,如果目标是通知同一事物的多个对象,那听起来更像是 NSNotification 架构或类似架构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM