I'm new to Swift and I'm trying to rewrite a callback as delegation with typealias and I am lost :(
Here is my code:
protocol NewNoteDelegate: class {
typealias MakeNewNote = ((String, String) -> Void)?
}
class NewNoteViewController: UIViewController {
@IBOutlet weak private var titleField: UITextField?
@IBOutlet weak private var noteField: UITextView!
weak var delegate: NewNoteDelegate?
// public var makeNewNote: ((String, String) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
titleField?.becomeFirstResponder()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .done, target: self, action: #selector(didTapSave))
}
@objc func didTapSave() {
if let text = titleField?.text, !text.isEmpty, !noteField.text.isEmpty {
// makeNewNote?(text, noteField.text)
delegate?.MakeNewNote(text, noteField.text)
}
}
}
The errors are:
- Cannot convert value of type 'String' to expected argument type '(String, String) -> Void'
- Extra argument in call
The original optional callback definition and call is commented out. I first tried rewriting makeNewNote
as a typealias without the protocol but still got the same errors.
I also tried removing ?
from MakeNewNote
but that yielded a new error:
- Type '(String, String) -> Void' has no member 'init'
I tried a lot of googling and it's been hours. Can anyone help me figure out what's wrong or point me in the right direction? Thanks in advance.