Delegation of singleton class in Swift
First, I'd like to point to:
1- Creating a singleton Class should contains:
private init() {}
That leads to enforce to access the class only by using its shared instance.
2- As mentioned in Max's Answer, the delegate should be optional and has a weak reference.
3- The protocol should be a type of class, as follows:
protocol AClassDelegate: class
For more information, you might want to check this Q&A.
Now, let's assume -for testing purposes- that the method1()
from AClassDelegate
should be get called when calling foo()
from A
class:
protocol AClassDelegate: class {
func method1()
}
class A {
private init() {}
static let shared = A()
weak var delegate: AClassDelegate?
func foo() {
print(#function)
delegate?.method1()
}
}
Let's implement classes that conforms to AClassDelegate
:
class B: AClassDelegate {
func bar() {
A.shared.delegate = self
A.shared.foo()
}
func method1() {
print("calling the delegate method B Class")
}
}
class C: AClassDelegate {
func bar() {
A.shared.delegate = self
A.shared.foo()
}
func method1() {
print("calling the delegate method C Class")
}
}
Output should be:
let b = B()
b.bar()
// this will print:
/*
foo()
calling the delegate method B Class
*/
let c = C()
c.bar()
// this will print:
/*
foo()
calling the delegate method C Class
*/
Swift Delegate set on a singleton at 2 places
The delegate design pattern is a to-one pattern. Any given object only has one delegate.
That said, it wouldn't be that hard to create your own multi-delegate pattern. Give the object that you want to have several delegates (multiDelegates, let's call them) an array of delegate objects. Have its init method create an empty array multiDelegates
.
Give the object that manages a multiDelegate array an addDelegate method and a removeDelegate method. in fact, create a MultiDelegateProtocol that defines these methods.
In the GameCenterFacade, when it goes to notify clients about interesting events, have it loop through it's array of multiDelegates and notify each one in turn.
Alternately, you could use a publish/subscribe pattern, or Notification Center.
How do i make singleton class as delegator in swift3
Make sure you are communicating with the singleton properly. To set your instance of LoginViewController
as the empLoginDelegate
, call:
CommunicationModule.sharedInstance.empLoginDelegate = self
from a method inside of LoginViewController
.
delegate for a singleton object
No, a delegate wouldn't fail, but consider using NSNotificationCenter instead:
static NSString *const kMyClassNotificationName = @"myClassNotificationName";
// where you would call a delegate method (e.g. [self.delegate doSomething])
[[NSNotificationCenter defaultCenter] postNotificationName:kMyClassNotificationName object:self userInfo: /* dictionary containing variables to pass to the delegate */];
// where you would set up a delegate (e.g. [Singleton instance].delegate = self)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething) name:kMyClassNotificationName object:[Singleton instance]];
Set delegate in static method swift
self inside static method refers to the type itself (ScalesHelper in your case)
Maybe you need to convert ScalesHelper into singleton class. Then you will not need a static function.
Here is how to make a singleton class for your reference
https://developer.apple.com/documentation/swift/cocoa_design_patterns/managing_a_shared_resource_using_a_singleton
Relation between Singleton class & AppDelegate class in iOS Objective-C
AppDelegate is however a singleton class but you show only use it for declaring things that applies globally in your application.
For ex: If you want to change the color of navigation bar in your entire application you can use app delegate and set the color of navigation bar. Also app delegate is an object that handles different state transition in your app.
So if you want to create a variable that can be changed from multiple View controllers you should create a singleton class and declare that variable in that class.
Singleton Manager initWithDelegate
I found the solution:
static Manager2 *sharedInstance = nil;
- (instancetype)init
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = self;
});
return sharedInstance;
}
+ (Manager2 *)sharedInstanceWithDelegate: (id)aDelegate
{
if (aDelegate != nil)
[sharedInstance setDelegate:aDelegate];
return sharedInstance;
}
A simple init and then, send the delegate when I get the instance.
Related Topics
Reversing a Range Results in Mismatching Types
How to Use Urlsession with Proxy in Swift 3
What Is the Simplest Way to Retrieve the Device Serial Number of an iOS Device Using Monotouch
iOS Swift Flood Fill Algorithm
What Is the Purpose of Firebase Appcheck
How to Add Two or More Buttons to Annotationview: Mkannotationview
How to Load Thousands Records to Realm Correctly
Increase the Width of Content View Programmatically in Swift
How to Update iOS 14 Widget Background Color from the App
How to Change Pin Color in Mapkit Under the Same Annotationview (Swift3)
Swift: Sort Array by Sort Descriptors
Can't Copy File from Bundle to Documents Directory in iOS
Save Video to a Custom Album Using Photos Framework in iOS
How to Send Request from iOS (Swift) to Dialogflow V2 API Without Cloud Functions