美文网首页程序员
Swift学习:Delegate

Swift学习:Delegate

作者: Think_cy | 来源:发表于2020-08-07 07:59 被阅读0次

Delegate

Delegate也是iOSMac OSX开发中最长使用的一种设计模式,其主要作用是将一个对象方法的实现,交给另外一个对象去实现。

Objective-C

定义一个protocol

// 定义一个协议
@protocol CYViewDelegate <NSObject>
- (void)changeContent:(NSString * _Nullable)content; 
@end
  
 // 类CYView持有这个delegate
@interface CYView : NSObject
@property (nonatomic, weak) id<CYViewDelegate> delegate;
@end

调用协议的地方

// 调用CYViewDelegate的方法
- (void)mouseDown:(NSEvent *)event {
    NSString *content = @"Hello DUDU!";
    // 注意这里的判断逻辑
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeContent:)]) {
        [self.delegate changeContent:content];
    }
}

实际执行该delegate方法的类

@interface AppDelegate () <CYViewDelegate>
  
@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet CYView *customView;
@property (weak) IBOutlet NSTextField *contentLabel;

@end
@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    self.customView.delegate = self;
}

// 实际执行delegate的地方
- (void)changeContent:(NSString *)content {
    self.contentLabel.stringValue = content;
}

@end

Swift

这次主要来学习如何使用Swift中的delegate。

定义一个delegate

protocol CYViewDelegate: class {
    func changeContent(_ content: String)
}

class CYView: NSView {
    weak var delegate: CYViewDelegate?
  
    override func mouseDown(with event: NSEvent) {
        print("鼠标左键按下")
        let content = "Hello, Kamal!"
        delegate?.changeContent(content)
    }
}

注意:

  1. 这里我们定义协议的时候,在CYViewDelegate后面加了class的。这里是为了保证protocol只能在类中使用(Swiftstructenum都可以)。也可以如下:
// 1. @objc是表明当前代码是针对NSObject对象,也就是class对象,就可以正常使用weak了
// 2. 仅@objc标记的protocol可以使用@optional。
@objc protocol CYViewDelegate {
    @objc optional func changeContent(_ content: String)
}
  1. 只有delegateclass的时候,才需要加weak修饰
  2. 并且weak的引用是optional的,所以必须使用var修饰,并且可以为nil
  3. delegateoptional的,那么不需要Objective-C中的respondsToSelector判断了

实现这个delegate

这里就和Objective-C中的写法差不多了。

class AppDelegate: NSObject, NSApplicationDelegate, CYViewDelegate {
        @IBOutlet weak var postButton: NSButton!
    @IBOutlet weak var customView: CYView!
    @IBOutlet weak var contentLabel: NSTextField!
  
      func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        self.customView.delegate = self
    }
  
      func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
    
    // 实际执行delegate方法的地方
      func changeContent(_ content: String) {
        self.contentLabel.stringValue = content
    }
}

总结

  1. 使用关键词weak是为了避免强引用导致的闭环。

  2. delegate是一个类时,使用weak关键词。Swift中的structenums是值对象类型,不是引用类型,所以它们不会引起强引用闭环

相关文章

  • Swift学习:Delegate

    Delegate Delegate也是iOS和Mac OSX开发中最长使用的一种设计模式,其主要作用是将一个对象方...

  • swift delegate 和 block 使用

    swift delegate 和 block 使用 delegate使用 //自定义cell 代码importUI...

  • delegate

    Swift的delegate 用weak修改的时候的注意事项Swift-代理

  • Swift小知识

    1. 关于Swift中Protocol 1. 在 Swift 中,Delegate 就是基于 Protocol 实...

  • swift delegate

  • Swift delegate

    ARC 中,对于一般的 delegate,在声明中将其指定为 weak,在这个 delegate 实际的对象被释放...

  • Swift - Delegate

    在ARC中,对于一般的delegate,我们会在声明中将其指定为weak,在这个delegate实际的对象被释放的...

  • About iOS programming: Delegate

    关于Delegate设计模式. From Hacking with Swift, Project 4. Deleg...

  • swift delegate 使用

    1、声明一个deleagte @objc protocol MVPTabBarDelegate : NSObjec...

  • Swift weak delegate

    需要使用delgate时,为了防止循环引用需要添加weak关键字,但是上面的代码XCode会报错。因为swift里...

网友评论

    本文标题:Swift学习:Delegate

    本文链接:https://www.haomeiwen.com/subject/iobxdktx.html