Why use delegate / protocol ?

I think this is more than 1 question but I don't see why threads should be limited.


First off, can a Protocol be used without a Delegate ?


So from example, if I have a ...

ViewController that inherits "doThisProtocolDelegate" {

and conforms to variables / functions,

also has an instance of WhateverClass.

}

then the instanced WhateverClass {

that has "thisDelegate: doThisProtocolDelegate"

then some function using "thisDelegate.(var/func)"

}

Which is suppose to pass to the ViewController (the parent) because it has inherited the Protocol, only by setting in ViewController "WhateverClass.thisDelegate = self". A question here is, can the Delegate be set to something other than self ? Also is it possible to do this without an instance of a sub Class within the main Class ?


This might clear a bit up, for example ...


ViewController {

that has an instance of a CustomView

and AnotherViewController

}

CustomView {

has "thisDelegate: doThisProtocolDelegate"

etc.

}

AnotherViewController inherits the Protocol {

conforming ...

}


So getting CustomView & AnotherViewController to talk via the Protocol ? Also where to set the Delegate ?


Another way of doing this without a Protocol is to have a Parent reference. So AnotherViewController has a variable "parent_VC" which exposes ViewController's variables & functions, also one in CustomView then just pass things along via their parent. I got the method from using Unreal Engine and not sure if it's exactly the same, also if there is a reason to not do this hence why people use a Protocol ?

First off, can a Protocol be used without a Delegate ?


Yes. delegate is when you use delegation only (sorry for this trivial answer).


here is an example

protocol ExampleProtocol {
    var simpleDescription: String { get }
    mutating func adjust()
}


class SimpleClass: ExampleProtocol {
    var simpleDescription: String = "A very simple class."
    var anotherProperty: Int = 69105
    func adjust() {
        simpleDescription += "  Now 100% adjusted."
    }
}

Extrait de: Apple Inc. « The Swift Programming Language (Swift 4). » iBooks.


No delegate there.


But in all your examples, you describe protocols with delegates, hence some confusion.

I don't mean to be a simpleton but It's a bit vague. Is the funcion being called from SimpleClass ?


As far as I can figure Protocol's only bridge when setting a Delegate ? I think I may have this all wrong, probably need to go back to the drawing board. It's been well over a year since I used Unreal Engine I think It's C++ based and I mostly used the Visual Scripting, however there is a bridging method called a Blueprint Interface ... https://youtu.be/G_hLUkm7v44 Is there anything similar to this using Swift ?

That was just an asnwer to the first question, with an illustration.


Is the function being called from SimpleClass ?


What do you mean ?

What it does is to allow you to write this:


let myClass = SimpleClass()

print(myClass.simpleDescription)


For the second question, sorry, but it is a bit difficult to undertsand exactly your question. Should you post real code, it could be easier.

Sorry I got things a bit wrong, I've updated my last post.

The answer depends on the context. Some APIs require a delegate, others do not.


Also, a class does not "inherit" from a protocol. A class "inherits" from a base class. A class "implements" a protocol.


A delegate does not have to be "self". It can be any objects that implements the protocol. Usually, you are going to want to have some context to those delegate method calls, so "self" is often the best choice. But sometimes, you might want to have a helper class as a delegate to keep whatever "self" is a little less messy.

Sorry, it is still not easy to understand your post.


But, from what you said :

So getting CustomView & AnotherViewController to talk via the Protocol ? Also where to set the Delegate ?


ViewController {

that has an instance of a CustomView

and AnotherViewController

}


The delegate shiould be set when you create the instance in ViewController


let customVC = CustomView()
customVC.delegate = self // If you want ViewController to be the delegate
let anotherVC = AnotherViewController()
anotherVC.delegate = self // If you want ViewController to be the delegate

Once again sorry for the misunderstanding and thanks for the example of a Protocol, I did understand you were only giving an answer to the first part, not sure why I wrote the rest. I've had a look at uses of Protocols without a Delagete and understand them a little more. I just forgot to add the setting of the Delegate in the example.

Whatever I tried at the time actually did work when setting the Delegate to something other than self I over looked what I tried at the time. My first look at a Protocol with a Delegate just appeared as if it was a method of communication between Classes and asumed the rest. Not sure if this is what you mean by a "helper class" but I have considered using a Singleton although apparently they should not be used.


"method called a Blueprint Interface ... https://youtu.be/G_hLUkm7v44 Is there anything similar to this using Swift ?"


No idea if you're familiar with Unreal Engine but with the Interface Class looks as if it's set up the same way. I'm trying to look at different ways to communicate between classes.

I'm not sure what you are saying. A helper class is just that - a new class, instantiated as an object, that encapsulates logic that would otherwise get in the way. It could be a singleton. I am unaware of any reason why singletons should not be used.


Sorry, I am not going to watch any videos.


I suggest putting Unreal engine aside for a few months or years and focus on learing more software development.

I am unaware of any reason why singletons should not be used.

Some consider singletons to be an anti-pattern, for various different reasons. You can find a lot of opinions about this by searching the ’net for singleton anti-pattern.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

eskimo wrote:


Some consider singletons to be an anti-pattern, for various different reasons. You can find a lot of opinions about this by searching the ’net for singleton anti-pattern.

I am aware that there are a lot of opinions on the internet.

Why use delegate / protocol ?
 
 
Q