Trying to understand the implementation of delegates with protocols in Swift - ios

After doing loads of research I am still a little confused about how to use and implement delegates. I have tried writing my own, simplified example, to aid my understanding - however it does not working - meaning I must be a little lost.
//the underlying protocol
protocol myRules {
func sayName(name: String);
}
//the delegate that explains the protocols job
class myRulesDelegate: myRules {
func sayName(name: String){
print(name);
}
}
//the delegator that wants to use the delegate
class Person{
//the delegator telling which delegate to use
weak var delegate: myRulesDelegate!;
var myName: String!;
init(name: String){
self.myName = name;
}
func useDels(){
//using the delegate (this causes error)
delegate?.sayName(myName);
}
}
var obj = Person(name: "Tom");
obj.useDels();
I have read and watched so many tutorials but am still struggling. I no longer get error (cheers guys). But still get no output from sayName.
which demonstrates I must be misunderstanding how delegate patterns work.
I would really appreciate a corrected version of the code, with a simple explanation as to why it works, and why it is helpful.
I hope this helps others too. Cheers.

In Swift you omit the first parameter's external name, so your function call should be delegate.sayName("Tom")
Also, it is dangerous to use an implicitly unwrapped optional for your delegate property, as you have found. You should use a weak optional:
//the underlying protocol
protocol MyRulesDelegate: class {
func sayName(name: String)
}
//the delegator that wants to use the delegate
class Person {
//the delegator referencing the delegate to use
weak var delegate: MyRulesDelegate?
var myName: String
init(name: String){
self.myName = name
}
func useDels() {
//using the delegate
delegate?.sayName(myName)
}
}
Finally, your delegate must be an object, so you can't use a delegate in the way you have shown; you need to create another class that can set an instance of itself as the delegate
class SomeOtherClass: MyRulesDelegate {
var myPerson: Person
init() {
self.myPerson = Person(name:"Tom")
self.myPerson.delegate = self
}
func sayName(name: String) {
print("In the delegate function, the name is \(name)")
}
}
var something = SomeOtherClass()
something.myPerson.useDels()
Output:
In the delegate function, the name is Tom

Related

What exactly happens when you assign self to delegate?

I'm new to Swift and I'm having a hard time understanding the purpose of assigning self to a delegate. Part of the difficulty stems from the fact that delegate seems to be used in two different ways.
First is as means to send messages from one class to another when a specific event happens, almost like state management. Second is to enable "a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type," as stated in documentation. I have a feeling that these two are fundamentally the same and I'm just not getting it.
protocol PersonProtocol {
func getName() -> String
func getAge() -> Int
}
class Person {
var delegate: PersonProtocol?
func printName() {
if let del = delegate {
print(del.getName())
} else {
print("The delegate property is not set")
}
}
func printAge() {
if let del = delegate {
print(del.getAge())
} else {
print("The delegate property is not set")
}
}
}
class ViewController: UIViewController, PersonProtocol {
var person: Person!
override func viewDidLoad() {
person.delegate = self
person.printAge()
person.printName()
}
func getAge() -> Int {
print("view controller")
return 99
}
func getName() -> String {
return "Some name"
}
}
What is the purpose of person.delegate = self in this case? Isn't ViewController already required to conform to PersonProtocol without it?
I have a feeling that these two are fundamentally the same
The first is a special case of the second. "send messages from one class to another" is just a specific way of "handing off some of its responsibilities". The "messages" are the "responsibilities"
What is the purpose of person.delegate = self in this case?
Here, person delegates (i.e. hands off) some of its responsibilities to another object. It does this by sending messages to another object. First, it needs to identify which objects it can delegate these responsibilities to. This is achieved by requiring that its delegate conform to PersonProtocol, as PersonProtocol defines the messages that Person is going to send.
Next, person needs to know exactly which object it should send these messages to. This is what person.delegate = self does. Remember that person doesn't know anything about your ViewController until this point. Instead of = self, you could say:
person.delegate = SomeOtherClassThatConformsToPersonProtocol()
and person will send its messages to that object instead, and the methods in your ViewController won't be called.
Isn't ViewController already required to conform to PersonProtocol without it?
Correct, but without it, person doesn't know which object it should send its messages to, and as a result, the methods in your ViewController won't be called.
Note that the delegate property should be declared as weak to avoid retain cycles. When you do person.delegate = self, you get a retain cycle: self has a strong reference to person, person also has a strong reference to self via the delegate property.
If you notice inside your Person class, delegate is nil. If you don't execute person.delegate = self, delegate will remain nil.
In other words, assigning ViewController to person.delegate allows Person to identify who the delegate is (i.e., have a reference to ViewController), and that way you can successfully execute statements like delegate?.getName() or delegate?.getAge() from the Person class.
that means Person is not able to getName() and getAge() so Person class delegate that to other DataSource.
Lets say the your view controller has a data source class PersonDataSource which deal with API to get this information So
class PersonDataSource: PersonProtocol {
func getAge() -> Int {
print("view controller")
return 99
}
func getName() -> String {
return "Some name"
}
}
so the view controller will looks like this
class ViewController: UIViewController {
var person: Person!
var personDataSource = PersonDataSource()
override func viewDidLoad() {
person.delegate = personDataSource
person.printAge()
person.printName()
}
}

How can assing generic (associated type require) protocol as delegate to any controller?

I have a protocol that have associatedtype named MessageType.
protocol MessageProtocol: class {
associatedtype MessageType
func sendMessage(_ with: MessageType)
}
Then implemented it in the controller
extension MainController: MessageProtocol{
typealias MessageType = String
func sendMessage(_ with: MessageType) {
// sending message
}
}
My purpose is using the protocol as delegate in other controller like below.
final class AnotherController {
weak var messagerDelegate: MessageProtocol?
...
}
But I get error of that
Protocol 'MessageProtocol' can only be used as a generic constraint
because it has Self or associated type requirements
Is there any way to handle this error?
I've reading Big Nerd Ranch blog post about this situation.
https://www.bignerdranch.com/blog/why-associated-type-requirements-become-generic-constraints/
I've learned about the situation but no idea about how it can be achived?
Thanks,
I handle the situation like that.
final class AnotherController<T: MessageProtocol> where T.MessageType == String {
weak var messagerDelegate: T?
...
}
And if I want to create anotherController instance programmatically, I created like that.
let instance = AnotherController<MainController>(frame: CGRect.zero)
instance.delegate = self
...
Because of MainController is comfortable with MessageProtocol from
extension MainController: MessageProtocol{}
Probably it's not common case but worked for me.

passing data to already implemented delegate methods inside extensions inside swift

I am trying to pass data to a delegate method implemented inside an extension but i am unable to do it since extensions cannot have stored properties. How to get it done?
You can make the stored property a requirement of the delegate protocol.
protocol MyProtocol {
var aProperty: String { get set }
func aProtocolMethod()
}
For the corresponding extension of MyProtocol, the property can be accessed directly.
extension MyProtocol {
func aProtocolMethod() {
print("property:" + aProperty)
}
}
In the class which conforms to MyProtocol, it should implement the variable to store data.
class MyClass: MyProtocol {
var aProperty: String
init() {
self.aProperty = "some value"
}
}
let myClass = MyClass()
myClass.aProtocolMethod()

How to move data in VC using Delegates?

How can I move the data stored in data into the next VC and append it in my list when the sendDate is tapped ? Here is my code of the sending class :
protocol DataSentDelegate{
func userDidEnterData(data: String)
}
class SecondViewController: UIViewController{
var delegate: DataSentDelegate!
#IBAction func addItem(_ sender: Any)
{
let data = textField.text
delegate?.userDidEnterData(data: data)
}
Here is the code of recieving class :
class SecondPageViewController: UIViewController, DataSentDelegate{
func userDidEnterData(data: String) {
}
#IBAction func sendDate(_ sender: UIDatePicker) {
}
How do I implement list.append(data!) where data holds the value of textField.text
Do you not have to set the delegate in SecondPageViewController to self (normally in ViewDidLoad?
You declared it but don't seem to have assigned it.
You can share data through whole project using Singleton Pattern.
A singleton class is initialised for only once.
Look at these answers:
Swift - set delegate for singleton
What about you just add an array variable in your SecondPageViewController which will hold a list of strings, then append a new string each time sendDate delegate method gets called?
A few other remarks, there is no need to declare your delegate var as implicitly unwrapped if you're using optional chaining anyway, just declare it as an optional. Secondly, because SecondPageViewController is a class, it's better to make your delegate protocol class bound as such: protocol DataSentDelegate: class { func userDidEnterData(data: String) }, thirdly to avoid possible strong reference cycles make your delegate var a weak one.

Compiler error when assigning the Delegate for a Protocol in Swift iOS

I have a problem assigning the delegate for an object that is an instance of a class that defines a protocol in Swift as follows:
I simplified the code to the bare bones to exemplify the issue:
This is the class with the protocol
protocol TheProtocol {
func notifyDelegate()
}
class ClassWithProtocol: NSObject {
var delegate: TheProtocol?
fire() {
delegate?.notifyDelegate()
}
}
This is the class the conforms to the Protocol
class ClassConformingToProtocol: NSObject, TheProtocol {
var object: ClassWithProtocol?
func notifyDelegate() {
println("OK")
}
init() {
object = ClassWithProtocol()
object?.delegate = self // Compiler error - Cannot assign to the result of this expression
object?.fire()
}
}
I have tried all sort of alternatives to assign the delegate without success. Any idea what I am missing?
The Known Issues section of the Release Notes says:
You cannot conditionally assign to a property of an optional object.
(16922562)
For example, this is not supported:
let window: NSWindow? = NSApplication.sharedApplication.mainWindow
window?.title = "Currently experiencing problems"
So you should do something like if let realObject = object { ... }

Resources