Understanding the Static keyword in Swift | by Herbie Dodge | Nerd For Tech | Medium

image

Photo by Safar Safarov on Unsplash

image

Object oriented programming languages like Swift give us the power and flexibility to model code to solve problems. By grouping properties and methods together we are able to modularize code into custom objects to suit any need. When it comes to building these blueprints, properties and methods are grouped together to solve a specific task.

More often than not, when we configure an object, its properties are set during the instance’s initialization. These properties and their values are set and associated with that specific instance. Copies of the object could have identical properties or different values. Methods associated with an object can only be called when an instance of the object is created in memory; for the most part that satisfies most situations.

Imagine we are making a networking layer for a project; our custom class would consist of a method to download some data from the internet, and other methods/properties to manage data entering our app. (For the sake of this example, we will focus on the configuration of our NetworkManager rather than any actual implementation.)

image

Our NetworkManager has two methods for downloading and sending data. Like any other class we cannot simply call these methods without first creating an instance of NetworkManager, and then calling any methods.

image

Attempting to call any methods of NetworkManager directly without an instance throws an error stating: Instance member ‘fetchData’ cannot be used on type ‘NetworkManager’; did you mean to use a value of this type instead?

What is Xcode telling us? Well, methods like fetchData() and sendData() can only be called when they are the value of a type ‘NetworkManager’, in other words a constant or variable that is an instance of NetworkManager.

Our NetworkManager’s methods are only allocated into memory when we create an instance of NetworkManger, creating an instance prepares our object for use.

image

While this definitely works fine, there is a better way. Imagine your favorite app on your device that uses an internet connection, throughout the code it is probable that network calls are being made in multiple places: through different screens, events, etc. Networking this way creates multiple lines of the same code as well as objects in memory to be managed. There is an easy keyword to solve this problem.

By adding the Static keyword to an object’s properties and methods we can use them without the need of creating an instance first. (note that both variables and constants can be static as well.)

image

Now that our methods are properly prefaced we can use them without creating an instance of NetworkManager. To use them, simply call on the type and use dot notation to specify the method.

image

So what is exactly going on here? Why isn't Xcode throwing an error referring to value types? By declaring properties and methods as Static, Swift allocates them directly into the object’s memory, making it available for use without the need of an instance.

The Static keyword also allows for the use of the Singleton pattern: a Singleton is an object that references an instance of itself, by doing so eliminating the need for managing multiple objects throughout an app and the risk of memory leaks due to improper object referencing causing memory leaks.

Our NetworkManger could be configured as a Singleton by adding a property contaning an instance of NetworkManger. We will name this property shared, which is the convention for Singleton instance naming.

image

It is important to note that when using the Singleton pattern it is not necessary to create any instances directly. Now that our Singleton is setup we can remove the static keywords from our fetchData() and sendData().

Now to use our new Singleton we simply use dot notation to specify we want to call methods using the shared instance of NetworkManager

image

The Static keyword makes it easier to utilize an objects properties or methods without the need of managing instances. Use of the Static keyword in the Singleton pattern can reduce memory leaks by mismanaging instances of classes.

Photo by Safar Safarov on Unsplash

Object oriented programming languages like Swift give us the power and flexibility to model code to solve problems. By grouping properties and methods together we are able to modularize code into custom objects to suit any need. When it comes to building these blueprints, properties and methods are grouped together to solve a specific task.

More often than not, when we configure an object, its properties are set during the instance’s initialization. These properties and their values are set and associated with that specific instance. Copies of the object could have identical properties or different values. Methods associated with an object can only be called when an instance of the object is created in memory; for the most part that satisfies most situations.

Imagine we are making a networking layer for a project; our custom class would consist of a method to download some data from the internet, and other methods/properties to manage data entering our app. (For the sake of this example, we will focus on the configuration of our NetworkManager rather than any actual implementation.)

Our NetworkManager has two methods for downloading and sending data. Like any other class we cannot simply call these methods without first creating an instance of NetworkManager, and then calling any methods.

Attempting to call any methods of NetworkManager directly without an instance throws an error stating: Instance member ‘fetchData’ cannot be used on type ‘NetworkManager’; did you mean to use a value of this type instead?

What is Xcode telling us? Well, methods like fetchData() and sendData() can only be called when they are the value of a type ‘NetworkManager’, in other words a constant or variable that is an instance of NetworkManager.

Our NetworkManager’s methods are only allocated into memory when we create an instance of NetworkManger, creating an instance prepares our object for use.

While this definitely works fine, there is a better way. Imagine your favorite app on your device that uses an internet connection, throughout the code it is probable that network calls are being made in multiple places: through different screens, events, etc. Networking this way creates multiple lines of the same code as well as objects in memory to be managed. There is an easy keyword to solve this problem.

By adding the Static keyword to an object’s properties and methods we can use them without the need of creating an instance first. (note that both variables and constants can be static as well.)

Now that our methods are properly prefaced we can use them without creating an instance of NetworkManager. To use them, simply call on the type and use dot notation to specify the method.

So what is exactly going on here? Why isn't Xcode throwing an error referring to value types? By declaring properties and methods as Static, Swift allocates them directly into the object’s memory, making it available for use without the need of an instance.

The Static keyword also allows for the use of the Singleton pattern: a Singleton is an object that references an instance of itself, by doing so eliminating the need for managing multiple objects throughout an app and the risk of memory leaks due to improper object referencing causing memory leaks.

Our NetworkManger could be configured as a Singleton by adding a property contaning an instance of NetworkManger. We will name this property shared, which is the convention for Singleton instance naming.

It is important to note that when using the Singleton pattern it is not necessary to create any instances directly. Now that our Singleton is setup we can remove the static keywords from our fetchData() and sendData().

Now to use our new Singleton we simply use dot notation to specify we want to call methods using the shared instance of NetworkManager

The Static keyword makes it easier to utilize an objects properties or methods without the need of managing instances. Use of the Static keyword in the Singleton pattern can reduce memory leaks by mismanaging instances of classes.

Photo by Safar Safarov on Unsplash

Object oriented programming languages like Swift give us the power and flexibility to model code to solve problems. By grouping properties and methods together we are able to modularize code into custom objects to suit any need. When it comes to building these blueprints, properties and methods are grouped together to solve a specific task.

More often than not, when we configure an object, its properties are set during the instance’s initialization. These properties and their values are set and associated with that specific instance. Copies of the object could have identical properties or different values. Methods associated with an object can only be called when an instance of the object is created in memory; for the most part that satisfies most situations.

Imagine we are making a networking layer for a project; our custom class would consist of a method to download some data from the internet, and other methods/properties to manage data entering our app. (For the sake of this example, we will focus on the configuration of our NetworkManager rather than any actual implementation.)

Our NetworkManager has two methods for downloading and sending data. Like any other class we cannot simply call these methods without first creating an instance of NetworkManager, and then calling any methods.

Attempting to call any methods of NetworkManager directly without an instance throws an error stating: Instance member ‘fetchData’ cannot be used on type ‘NetworkManager’; did you mean to use a value of this type instead?

What is Xcode telling us? Well, methods like fetchData() and sendData() can only be called when they are the value of a type ‘NetworkManager’, in other words a constant or variable that is an instance of NetworkManager.

Our NetworkManager’s methods are only allocated into memory when we create an instance of NetworkManger, creating an instance prepares our object for use.

While this definitely works fine, there is a better way. Imagine your favorite app on your device that uses an internet connection, throughout the code it is probable that network calls are being made in multiple places: through different screens, events, etc. Networking this way creates multiple lines of the same code as well as objects in memory to be managed. There is an easy keyword to solve this problem.

By adding the Static keyword to an object’s properties and methods we can use them without the need of creating an instance first. (note that both variables and constants can be static as well.)

Now that our methods are properly prefaced we can use them without creating an instance of NetworkManager. To use them, simply call on the type and use dot notation to specify the method.

So what is exactly going on here? Why isn't Xcode throwing an error referring to value types? By declaring properties and methods as Static, Swift allocates them directly into the object’s memory, making it available for use without the need of an instance.

The Static keyword also allows for the use of the Singleton pattern: a Singleton is an object that references an instance of itself, by doing so eliminating the need for managing multiple objects throughout an app and the risk of memory leaks due to improper object referencing causing memory leaks.

Our NetworkManger could be configured as a Singleton by adding a property contaning an instance of NetworkManger. We will name this property shared, which is the convention for Singleton instance naming.

It is important to note that when using the Singleton pattern it is not necessary to create any instances directly. Now that our Singleton is setup we can remove the static keywords from our fetchData() and sendData().

Now to use our new Singleton we simply use dot notation to specify we want to call methods using the shared instance of NetworkManager

The Static keyword makes it easier to utilize an objects properties or methods without the need of managing instances. Use of the Static keyword in the Singleton pattern can reduce memory leaks by mismanaging instances of classes.