为了账号安全,请及时绑定邮箱和手机立即绑定

代表们在斯威夫特?

代表们在斯威夫特?

收到一只叮咚 2019-06-24 15:45:05
代表们在斯威夫特?一个人如何去做一个代表?NSUserNotificationCenterDelegate在斯威夫特?
查看完整描述

3 回答

?
慕仙森

TA贡献1827条经验 获得超7个赞

这和obj-c没什么不同。首先,必须在类声明中指定协议,如下所示:

class MyClass: NSUserNotificationCenterDelegate

实现如下:

// NSUserNotificationCenterDelegate implementationfunc userNotificationCenter(center: NSUserNotificationCenter, 
didDeliverNotification notification: NSUserNotification) {
    //implementation}func userNotificationCenter(center: NSUserNotificationCenter, didActivateNotification notification: 
    NSUserNotification) {
    //implementation}func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: 
    NSUserNotification) -> Bool {
    //implementation    return true}

当然,您必须设置委托。例如:

NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self;


查看完整回答
反对 回复 2019-06-24
?
慕标5832272

TA贡献1966条经验 获得超4个赞

代表们总是把我搞糊涂直到我意识到委托只是一个为另一个类做一些工作的类。..这就像有别人为你做所有你不想做的肮脏工作。

我写了一个小故事来说明这一点。如果你愿意的话,可以在游乐场里读一读。

很久以前.。

// MARK: Background to the story// A protocol is like a list of rules that need to be followed.protocol OlderSiblingDelegate: class {
    // The following command (ie, method) must be obeyed by any 
    // underling (ie, delegate) of the older sibling.    func getYourNiceOlderSiblingAGlassOfWater()}
    // MARK: Characters in the storyclass BossyBigBrother {

    // I can make whichever little sibling is around at 
    // the time be my delegate (ie, slave)    weak var delegate: OlderSiblingDelegate?

    func tellSomebodyToGetMeSomeWater() {
        // The delegate is optional because even though 
        // I'm thirsty, there might not be anyone nearby 
        // that I can boss around.        delegate?.getYourNiceOlderSiblingAGlassOfWater()
    }}// Poor little sisters have to follow (or at least acknowledge) 
// their older sibling's rules (ie, protocol)class PoorLittleSister: OlderSiblingDelegate {

    func getYourNiceOlderSiblingAGlassOfWater() {
        // Little sis follows the letter of the law (ie, protocol),        // but no one said exactly how she had to respond.    
            print("Go get it yourself!")
    }}// MARK: The Story// Big bro is laying on the couch watching basketball on TV.let bigBro = BossyBigBrother()
    // He has a little sister named Sally.let sally = PoorLittleSister()// Sally walks into the room. How convenient! Now big bro 
// has someone there to boss around.bigBro.delegate = sally// So he tells her to get him some water.bigBro.tellSomebodyToGetMeSomeWater()
// Unfortunately no one lived happily ever after...// The end.

在评审中,编写和使用委托模式有三个关键部分。

  1. 这个

    协议

    ,它定义了工人需要做的事情。
  2. 这个

    老板班

    它有一个委托变量,它用来告诉Worker类要做什么。
  3. 这个

    工人阶级

    它采用了协议,并执行了所需的操作。

现实生活

与以上我们的波西老大哥故事相比,代表们经常被用于下列实际应用:

  1. 通讯

    一个类需要向另一个类发送一些信息。
  2. 定制化

    一个类希望允许另一个类自定义它。

最重要的是,这些类不需要事先了解彼此的任何信息,只需要委托类符合所需的协议。

我强烈建议阅读以下两篇文章。他们帮助我更好地理解了代表们文献资料做。

再来一个音符

引用它们不拥有的其他类的委托应该使用weak关键字以避免强引用周期。看见这个答案更多细节。


查看完整回答
反对 回复 2019-06-24
  • 3 回答
  • 0 关注
  • 458 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信