I am trying to create a Singleton class, where I want to create an instance of UIImage.
In Objective-C
we can simple declare a property in .h like
@property (nonatomic,strong)UIImage *pic;
and define a sharedSingleton Method in .m
+(SingletonClass*)sharedSingleton{
@synchronized(self){
if (!sharedSingleton) {
sharedSingleton=[[SingletonClass alloc]init];
}
return sharedSingleton;
}
}
and call from any class with
[SingletonClass sharedSingleton].pic
I am searching from last 2 hours but didn't find ant suitable tutorial to create this. please help me out to create a singleton class in swift and tell me how to call the instance variable.
its very simple in swift
class SharedManager {
static let sharedInstance = SharedManager()
var pic = UIImage()
}
and to access it
SharedManager.sharedInstance.pic = UIImage(named: "imagename")!
Here is very good guide about singleton
https://github.com/hpique/SwiftSingleton
https://thatthinginswift.com/singletons/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With