Static - when do we use it?
First, if you don’t know the difference between Type & Instance:
Type is the definition of a class \ struct .
Instance - is the creation \ a single show of that type.
for example (from Sean's Allen Channel):
The functions\variables belongs to the instance, meaning that we cannot access those functions\variables unless we create an instance.
if we want a function \ variable for the Type - we’ll use the static keyword.
in docs.swift.org we have an example of it’s use:
All of the game’s levels (apart from level one) are locked when the game is first played. Every time a player finishes a level, that level is unlocked for all players on the device. The LevelTracker structure uses type properties and methods to keep track of which levels of the game have been unlocked.
More examples from Sean’s Allen channel - using static to define constants like Colors, Fonts and Alerts for cleaner code:
领英推荐
enum Colors{
??? static let alunaBlue = UIColor(red: 108/255, green: 131/155, blue: 156/255, alpha: 1)
??? //add more colors…
}
enum Fonts {
??? static let spaceComics = "Space Comics"
??? //add more fonts…
}
enum Alert{
??? static func show(title: String, message: String, vc: UIViewController, andEnable button: UIButton? ){
??????? let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
??????? let action = UIAlertAction(title: "OK", style: .default) { _ in
??????????? if let button = button {
??????????????? button.isEnabled = true
??????????? }
??????? }
??????? alert.addAction(action)
??????? DispatchQueue.main.async {
??????????? vc.present(alert, animated: true)
??????? }
??? }
??? //add more custom alerts…
}
And how to use it:
class viewController : UIViewController {
??? let newColor = Colors.alunaBlue
??? let fontName = Fonts.spaceComics
??? override func viewDidLoad() {
??????? super.viewDidLoad()
??????? Alert.show(title: "Welcome!", message: "this is your dashboard!", vc: self, andEnable: nil)
??? }
}
Another way for static use - as a Singleton pattern, as shown in this example here:
class NetworkManager {
static let shared = NetworkManager()
func fetchData() {
//fetchData makes an API call to download data.
}
func sendData(){
//sendData makes an API Call to upload data.
}
}
and it's use:
class viewController : UIViewController{
??? override func viewDidLoad() {
??????? super.viewDidLoad()
??????? NetworkManager.shared.fetchData()
??? }
}
In conclusion:
use static to create a function \ method \ variable \ constant?which belongs to the Type and not the Type?Instance.
we will use it if we don’t need to instantiate an instance, or if we need a global func \ variable for the type (counter for example)
Backend Software Engineer - Developed and maintained the Backend infrastructures of commercial companies | Python | Django | FastAPI | AWS | AI (New post every Tuesday!)
2 年Interesting, thank you for sharing.
Back End Developer - Developing Infrastructures That Streamline Complex Processes And Create Personalized User Experiences | JavaScript | NodeJS | MongoDB | AWS | Docker | K8
2 年Great content!! Thanks for sharing ????
Unity Developer at @Qnigame - Creating VR Games, Casual Games and Strategic Games | Unity 3D | C# | Unreal Engine 4 | 5 | Blueprint | OOP | Design Patterns
2 年Thanks
Data Analyst –Extracting business insights from large datasets enabling various profit-maximizing actions. Assists athletes in determining their data-driven market value and how to increase it | Python | SQL | Excel
2 年thanks man very interesting !