TDLR; I have three classes, when Class A object is updated, it's calling on its delegate (Class B) which is calling on its delegate (Class C) without doing anything else. Class C will use Class B in different ways depending on the values in Class A. Class B need to know of its Class A at touch events. Is this acceptable?
classA { var information: String }
classB { var thing: ClassA thing.delegate = self }
classC { var things: [ClassB] for thing in things { thing.delegate = self } }
My real example
I have three classes: A mapViewController, a mapMarker and a place (model). The map holds multiple mapMarkers, and every mapMarker has a property place, which contains information of what the marker should look like (like place type, "bar", "restaurant" etc). The place might receive new information via a silent push notification, and hence being updated. When the place is updated, I need to notify the mapViewController that the marker needs to be redrawn (I'm using MapBox and their annotations doesn't support redrawing in any way but removing and adding the marker again, since the imageForAnnotation method is a delegate one.)
My first thought was to make two protocols placeDelegate and mapMarkerDelegate.
Place:
protocol PlaceDelegate: class
{
func placeUpdated()
}
class Place {
weak var delegate: PlaceDelegate?
var propertyThatCanBeUpdate: String {
didSet {
//Checking if the newValue == oldValue
delegate.placeUpdated()
}
}
MapMarker
protocol MapMarkerDelegate: class
{
markerShouldReDraw(mapMarker: MapMarker)
}
class MapMarker: PlaceDelegate {
var place: Place!
weak var delegate: MapMarkerDelegate?
init(place: Place) {
self.place = place
place.delegate = place
}
func placeUpdate()
{
delegate.markerShouldReDraw(self)
}
}
MapViewController
class MapViewController {
//I could easily set the marker.delegate = self when adding the markers
func markerShouldReDraw(mapMarker: MapMarker)
functionForRedrawingMarker()
}
This feels a bit ugly, and a bit weird that the MapMarker is just passing the "my place has been updated" information forward. Is this acceptable as far as performance goes? Should I use some kind of NSNotification instead? Should I make the MapViewController the delegate of place and search my array of mapMarker for the one holding the correct place?
Related
If delegates were designed for one-to-one relationships between objects and NSNotifications were designed for one-to-potentially-many relationships, is there a best practice for one-to-few?
I've seen a lot of custom multicasting delegates in iOS where an object can cast to multiple subscribers (i.e. Swift Language Multicast Delegate), but the implementations are often very involved and seem overkill. One such problem is safely storing an array of weak references (the delegates) (How do I declare an array of weak references in Swift?).
I've seen a lot of recommendations (like this one multiple listeners for delegate iOS) that suggest this is what NotificationCenter was made for. But the idea of broadcasting out into the ether for a one-to-few relationship itself seems overkill.
Is there a best practice for Apple's frameworks and the Swift language? I never see them write about this. Is NotificationCenter a suitable use for a one-to-few relationship where a multicasting delegate would otherwise be needed?
I would not use NotificationCenter because the type of the message and the data between the sender and receiver (observer) becomes lost. Using Notification Center will make your code to rely on Notification object where you need to use the userInfo dictionary of the notification to add the data which makes it harder to understand what exactly keeps the notification (will need to see how exactly the data is populated when the notification is send).
The delegate is a better solution and having more than 1 delegate in a weak list of delegates is ok. I have used such composition in many places where I need to register more then 1 listener to a particular event and works just fine.
You can create the delegates collection once and reuse it very easily across the code. Here is my solution:
class WeakContainer {
private weak var value: AnyObject?
public init(value: AnyObject) {
self.value = value
}
func get() -> AnyObject? {
return self.value
}
}
class DelegatesCollection<T>: Sequence {
private lazy var weakDelegates = [WeakContainer]()
var delegates: [T] {
return self.weakDelegates.map() { $0.get() as! T }
}
var hasDelegates: Bool {
return !self.weakDelegates.isEmpty
}
init() { }
func add(delegate: T) {
var exists = false
for currentDelegate in self.weakDelegates {
if(currentDelegate.get() === (delegate as AnyObject)) {
exists = true
break
}
}
if(!exists) {
self.weakDelegates.append(WeakContainer(value: delegate as AnyObject))
}
}
func remove(delegate: T) {
var i = 0
for currentDelegate in self.weakDelegates {
if(currentDelegate.get() == nil || currentDelegate.get() === (delegate as AnyObject)) {
self.weakDelegates.remove(at: i)
break
}
i += 1
}
}
func makeIterator() -> IndexingIterator<[T]> {
return self.delegates.makeIterator()
}
}
I can speculate that Apple frameworks use only single delegate because it is a business logic what actions to perform when delegate is called. From Apple's point of view it is enough to delegate that some event has happened and to leave the application to decide what to do next so there is no point to support multiple delegates on framework level.
Note: Sorry could not come-up with better title than this, so please
suggest a better one if you come across one after reading the question
I have a BasePresenter class, That should take BaseInteractor and BaseRouter as its init arguments, and each child class of BasePresenter should be able to specify subclass of BaseInteractor and BaseRouter in their implementation
So I have declared my BasePresenter as
open class PRBasePresenter<T: PRBaseInteractor, R: PRBaseRouter> {
var interactor: T!
var router: R!
let disposeBag = DisposeBag()
convenience init(with router : R, interactor : T) {
self.init()
self.router = router
self.interactor = interactor
}
}
So now PRBaseCollectionsPresenter which is a child of PRBasePresenter declares its interactor and router as
class PRBaseCollectionsPresenter: PRBasePresenter<PRBaseCollectionsInteractor, PRBaseCollectionRouter> {
//other code here
}
Obviously PRBaseCollectionsInteractor is a subclass of PRBaseInteractor and PRBaseCollectionRouter is a subclass of PRBaseRouter
Everything works till here fine. Now comes the issue. Every ViewController should have presenter as a property. So I have a protocol which mandates that with
protocol PresenterInjectorProtocol {
var presenter : PRBasePresenter<PRBaseInteractor, PRBaseRouter>! { get set }
}
And my BaseViewController confirms to PresenterInjectorProtocol
public class PRBaseViewController: UIViewController,PresenterInjectorProtocol {
var presenter: PRBasePresenter<PRBaseInteractor, PRBaseRouter>!
//other code...
}
Now lets say I have ChildViewController, it will obviously get presenter because of inheritance, but obviously child would want to have its specific presenter than having a generic one. And obviously in Swift when you override a property you cant change the type of the variable. So the only way is
class PRBaseTableViewController: PRBaseViewController {
var tableSpecificPresenter: PRBaseCollectionsPresenter {
get {
return self.presenter as! PRBaseCollectionsPresenter
}
}
//other code goes here
}
This gives me a warning
Cast from 'PRBasePresenter!' to
unrelated type 'PRBaseCollectionsPresenter' always fails
And trying to ignore it and running will result in crash :(
How can I solve this problem? What am I doing wrong? Or is this approach completely wrong?
So I have two ViewControllers. First (MapVC) with map and second (SettingsVC) with many settings that need to be applied to this map.
I thought it would be nice idea to create protocol like
protocol MapSettingsDelegate: class {}
I know that I can specify function inside this protocol. But how I should do it when I have many settings - how should I pass them from SettingsVC to MapVC.
Example:
struct MySettings {
var value1: String
var value2: String
// and so on...
}
protocol MapSettingsDelegate: class {
func settingsUpdated(newSettings: MySettings)
}
and implement it inside your controller
class MapVC : MapSettingsDelegate {
...
func settingsUpdated(newSettings: MySettings) {
// Update everything you need
}
...
}
Feel free to ask for details
I have a view controller that contains instances (objects) of a few related classes that need to intercommunicate.
I can easily set these up by creating instances of the objects within the VC:
var house = House ()
var road = Road ()
var town = Town ()
But how do I get a method in the House object to 'speak'/send data to the Road object? In some languages I could invoke the parent object and target the other class and its methods that way:
func sendHouseData (num: Int) {
_parent.road.getHouseData (num)
}
Obviously the _parent keyword is not swift code so won't work.
The only way I can think of doing this is to create an instance of road inside the House object as a child object, thus exposing it directly in scope to the method code. Having a lot of external code in the VC to do all the heavy lifting is another idea that seems like bad practice and inelegant.
Any ideas?
Many thanks.
Kw
One way would be to use the Delegation pattern. Each class should declare a protocol with all the messages it can send, and the class that needs to listen to those messages should implement the protocol, register as the (or one of the) delegate(s) and respond accordingly.
Here's a good example how to implement Delegation Pattern.
And a sample implementation for your objects:
protocol HouseDelegate {
func readData(data:Int)
}
class House {
var delegate:HouseDelegate
var data:Int = 0
init(delegate:HouseDelegate) {
self.delegate = delegate
}
func sendData() {
self.delegate.readData(data)
}
}
class Road: HouseDelegate {
var data:Int = 0
func readData(data: Int) {
print("Read data \(data)")
self.data = data
}
}
var road = Road ()
var house = House (delegate: road)
house.sendData() //prints "Read data 0"
Best way to handle this is by using mediator pattern, if you need to establish communication between House, Road, Town you can set it in a way that they communicate to 'mediator' (parent object). In this case parent object can mediate and coordinate between the objects
Or, you can use NSNotificationCenter, post a notification from one object and listen to the notification in another object, but this is hard to track as soon as your app gets more complex.
Let's say the Word class is parent class of the House, Road, Town.
Here you can establish communication between Word, House and Town, and inter-communication between House and Town.
Be advised this is more pseudo code I'm writting it in browser but you'll get idea
class Word {
let house = House()
let town = Town()
init() {
// Here World will be notified when the house hasReceivedVisitor and the town hasBuiltRoad.
self.house.hasReceivedVisitor = { visitorName in
print("\(visitorName) has visited the house!")
// Communication between house and town ...
self.town.doSometingWithVisitor(visitorName)
}
self.town.hasBuiltRoad = { roadNumber in
print("A road \(roadNumber) has been built!")
// Communication between town and house ...
self.house.doSometingWithRoad(roadNumber)
}
}
}
class House {
var hasReceivedVisitor: ((vistorName: String) -> ())?
func createVisitor() {
let vistor = Visitor("Mike")
self.hasReceivedVisitor?(vistor.vistorName)
}
func doSometingWithRoad(roadNumber: Int) {
// .....
}
}
class Town {
var hasBuiltRoad: ((roadNumber: Int) -> ())?
func createRoad() {
let road = Road(66)
self.hasBuiltRoad?(road.roadNumber)
}
func doSometingWithVisitor(visitorName: String) {
// .....
}
}
The same thing above can be accomplished via delegation but I like blocks more.
Basically you have 3 options: Notifications (the worst), delegation or callbacks.
In swift I'm implementing two protocols, GADCustomEventInterstitial and GADCustomEventBanner.
Both of these protocols require a property called delegate. delegate is a different type in each protocol, and thus a conflict arises.
class ChartBoostAdapter : NSObject, GADCustomEventInterstitial, GADCustomEventBanner, ChartboostDelegate{
var delegate:GADCustomEventInterstitialDelegate?; // Name conflict
var delegate:GADCustomEventBannerDelegate?; // Name conflict
override init(){
}
...
}
They are libraries/frameworks it's not my definition
Then obviously you cannot make the same class adopt both protocols. But you don't really need to. Just separate this functionality into two different classes, as is evidently intended by the designer of these protocols. You are supposed to have one class that adopts GADCustomEventInterstitial and has its delegate, and another class that adopts GADCustomEventBanner and has its delegate. What reason do you have for trying to force these to be one and the same class? As in all things where you are using a framework, don't fight the framework, obey it.
It is actually possible, I just encountered same situation. I had two different but kind of related protocols. In some cases I needed both to be implemented by delegate and in other cases only one and I didn't want to have two properties eg... delegate1, delegate2.
What you need to do is create another combined protocol that inherits from both protocols:
protocol ChartBoostAdapterDelegate: GADCustomEventInterstitialDelegate, GADCustomEventBannerDelegate { }
class ChartBoostAdapter : NSObject, GADCustomEventInterstitial, GADCustomEventBanner, ChartboostDelegate {
weak var delegate: ChartBoostAdapterDelegate?
override init(){
}
...
}
The simple answer is that you can't.
Maybe one protocol depends on another, in which case you would use the dependent protocol for the type of your delegate.
Note that this can be solved using Mixins (possible since Swift 2.0) if you are in a Swift-only environment. It just cannot be solved as long as you need to have the code bridged to Obj-C, as this problem is unsolvable in Obj-C. Yet that can usually be solved by a wrapper class, which I will show later on.
Let's break this down to a minimalist example:
import Foundation
#objc
protocol ProtoA {
var identifier: String { get }
}
#objc
protocol ProtoB {
var identifier: UUID { get }
}
#objc
class ClassA: NSObject, ProtoA, ProtoB {
let identifier = "ID1"
let identifier = UUID()
}
The code above will fail as no two properties can have the same name. If I only declare identifier once and make it a String, compiler will complain that ClassA does not conform to ProtoB and vice verse.
But here is Swift-only code that actually does work:
import Foundation
protocol ProtoA {
var identifier: String { get }
}
protocol ProtoB {
var identifier: UUID { get }
}
class ClassA {
let stringIdentifier = "ID1"
let uuidIdentifier = UUID()
}
extension ProtoA where Self: ClassA {
var identifier: String {
return self.stringIdentifier
}
}
extension ProtoB where Self: ClassA {
var identifier: UUID {
return self.uuidIdentifier
}
}
extension ClassA: ProtoA, ProtoB { }
Of course, you cannot do that:
let test = ClassA()
print(test.identifier)
The compiler will say ambigous use of 'identifier', as it has no idea which identifier you want to access but you can do this:
let test = ClassA()
print((test as ProtoA).identifier)
print((test as ProtoB).identifier)
and the output will be
ID1
C3F7A09B-15C2-4FEE-9AFF-0425DF66B12A
as expected.
Now to expose a ClassA instance to Obj-C, you need to wrap it:
class ClassB: NSObject {
var stringIdentifier: String { return self.wrapped.stringIdentifier }
var uuidIdentifier: UUID { return self.wrapped.uuidIdentifier }
private let wrapped: ClassA
init ( _ wrapped: ClassA )
{
self.wrapped = wrapped
}
}
extension ClassA {
var asObjCObject: ClassB { return ClassB(self) }
}
If you put it directly into the class declaration of ClassA, you could even make it a stored property, that way you don't have to recreate it ever again but that complicates everything as then ClassB may only hold a weak reference to the wrapped object, otherwise you create a retain cycle and neither of both objects will ever be freed. It's better to cache it somewhere in your Obj-C code.
And to solve your issue, one would use a similar wrapper approach by building a master class and this master class hands out two wrapper class, one conforming to GADCustomEventInterstitial and one conforming to GADCustomEventBanner but these would not have any internal state or logic, they both use the master class as storage backend and pass on all requests to this class that implements all required logic.