Hi,
To Create a Singleton class in swift follow steps below:
1- Add a Cocoa Class file subclass of NSObject named it as MySingleton and now write down the code as .
import UIKit
class MySingleton: NSObject {
static let sharedInstance = MySingleton()
func myMehtod(){
print("Calling the shared method here")
}
}
2- To call the myMehtod from ViewController write code as
@IBAction func myButtonTapped(sender:AnyObject?)
{
MySingleton.sharedInstance.myMehtod()
}
0 Comment(s)