我们为什么不使用其委托对象实例化委托类? [英] Why don't we instantiate delegating classes with their delegate objects?

查看:92
本文介绍了我们为什么不使用其委托对象实例化委托类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法想象委派的下载器类或CLLLocationManager或tableView没有委派。那么为什么我们需要将其创建为可选内容?

I can't imagine a delegating downloader class or CLLLocationManager or tableView be without a delegate. So why do we need create it as an optional?

为什么要这么做

tableview = UITableView()
tableview.delegate = self
tableview.dataSource = self

为什么不制作API,所以我们可以这样做:

Why isn't the API made so we could just do:

tableview = UITableView(delegate: self, dataSource: self)

这是因为内存循环,所以我们可以先 nil 是委托类还是委托类,然后是另一个?

Is this because memory cycles, so we could first nil either the delegating class or the delegate class and then the other? Is that the only reason?

即使是这种情况,使用

class tableView{
    var delegate: UITableViewDelegate?
    var dataSource: UITableViewDataSource?

    init(delegate: UITableViewDelegate? = nil, dataSource: UITableViewDataSource?){
        self.delegate = delegate
        self.dataSource = dataSource
    }
}


var tableView = tableView()


推荐答案

要求在实例化时声明的委托不会对语言本身带来任何实际好处。如果您确实感到需要,作为开发人员,您可以方便地添加一个便利,但是它不属于UIKit。

Requiring a delegate to be declared on instantiation adds no real benefit to the language itself. It's a convenience that you as the developer could easily add, if you really felt the need, but it doesn't belong in UIKit.

但是深入研究,您不会无论如何,总是要使用委托实例化tableview。如果委托人还不存在怎么办?代表并不总是自己。您可能需要使用属性来构建表视图,以便稍后显示或实现。

But digging deeper, you wouldn't always want to instantiate the tableview with a delegate anyway. What if the delegate doesn't exist yet? Delegates are not always self. You may need to build out your tableview with attributes for displaying or implementing at a later point.

您可能还想更改委托,对于读取代码的人来说,这可能会给调试增加不必要的混淆。考虑以下伪代码:

You may also want to change the delegate, which for someone reading code, could add a lot of unnecessary obfuscation to debugging. Consider the following pseudo-code:

var tableview = UITableView(delegate:self)

var childController = ChildControllerWithTableView()
childController.prepareForSegue() {
    childController.tableview = tableview
    childController.tableview.delegate = childcontroller
}

不仅您更改了委托,使未来的开发人员(可能包括您自己在内)感到困惑,而且还(不必要)将委托设置为 self ,仅仅是因为 childController 还不存在。

Not only have you changed the delegate, confusing future developers (probably yourself included!) but you also had to (unnecessarily) set the delegate to self, simply because childController didn't exist yet.

或者如乔什指出的那样,如果委托人是委托人,该怎么办?

Or what if the delegator is the delegate, as Josh points out?

var tableview = UITableView(delegate:tableview)

等等,什么?

不过,如果有业务需要(或方便),您可以将它添加到所有项目中!这就是让Swift如此出色的原因:

Still, you can add this to all your projects if you like - if there's a business need (or convenience) reason to do it! It's what makes Swift so awesome:

extension UITableView {
    convenience init(delegate: UITableViewDelegate? = nil, dataSource: UITableViewDataSource?){
        self.delegate = delegate
        self.dataSource = dataSource
    }
}

这篇关于我们为什么不使用其委托对象实例化委托类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆