How can I access a delegate through Swift?

I am trying to emulate an Objective-C template to access an object delegate using Swift. Normally I would put the protocol in .h, which is shared between two UIViewControllers.

Purpose: Call a delegate (host) to reject (push) the UIViewController.
Problem: Unable to access delegate greeting ().

The following codes compile, but I get an error at runtime: unknown delegation method (see below).

Host / Calling (Delegate) Object:

import UIKit

class MainViewController: UIViewController, ProtocolNameDelegate {                     
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    // ---------------------------------------------------------------------------------------------------------------
    // Protocol Delegate

    func hello() {
        println("Hello");
    }

    // ---------------------------------------------------------------------------------------------------------------

    @IBAction func greenAction(sender : AnyObject) { 
        let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("GreenViewController") as GreenViewController
        secondViewController.delegate = self;
        self.presentViewController(secondViewController, animated: true, completion: nil)
    }

    // ---------------------------------------------------------------------------------------------------------------

    @IBAction func exitAction(sender : AnyObject) {
        exit(0)
    }

An advanced (second or "green") UIViewController that needs to be fired:

import UIKit

@class_protocol protocol ProtocolNameDelegate {
    func hello()
}

class GreenViewController: UIViewController {
    weak var delegate: ProtocolNameDelegate?

    // ---------------------------------------------------------------------------------------------------------------

    @IBAction func returnAction(sender : UIBarButtonItem) {
        println("Inside returnAction")
        delegate?.hello()
    }  
}

Editorial: fixed delegate access to: delegate? .Hello () ... And restart the application. Below is the debugger:

(lldb) po delegate
Some
 {
  Some = {
    payload_data_0 = 0x0d110b90 -> 0x00009a78 (void *)0x00009b70: OBJC_METACLASS_$__TtC9RicSwift218MainViewController
    payload_data_1 = 0x0d110b90 -> 0x00009a78 (void *)0x00009b70: OBJC_METACLASS_$__TtC9RicSwift218MainViewController
    payload_data_2 = 0x00000000
    instance_type = 0x00000000
  }
}
(lldb) po delegate.hello()
error: <REPL>:1:1: error: 'ProtocolNameDelegate?' does not have a member named 'hello'
delegate.hello()

(lldb) po delegate?.hello()
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0xb3145b0).
The process has been returned to the state before expression evaluation.

: - ?

+4
3

, .

delegate?.hello()

, , nil

ProtocolNameDelegate? ProtocolNameDelegate, .

+1

. , Swift ( Xcode 6 beta 5). , . Objective-C @objc protocol ProtocolNameDelegate protocol ProtocolNameDelegate.

+1

Since the delegate is optional, you need to expand it, the fastest way would be to use an extra chain:

delegate?.hello()

You also need to make sure the delegate protocol is marked as @objc (instead of @class_protocol in this case)

0
source

Source: https://habr.com/ru/post/1544537/


All Articles