iOS Interview Question_Part5
.
1. What is static in swift?
static
properties and methods are belong to the class itself, rather than to instances of the class.
Here are some of the benefits of using static properties and methods:
- They can be accessed from anywhere in your code, without having to create an instance of the class.
- They can be used to store global data that is shared by all instances of the class.
- They can be used to implement the singleton pattern, which ensures that there is only one instance of a class created at a time.
Zombie Objects in Swift
Zombie Objects are something related to memory leaks.
Here are some examples of how zombie objects can be created:
- If you create a strong reference to an object and then never set the reference to nil, the object will never be deallocated and will eventually become a zombie.
- If you pass an object to a function or closure that retains the object, the object will not be deallocated until the function or closure is finished executing.
- If you create a circular reference between two objects, the objects will both become zombies.
A zombie object in Swift iOS is an object that has been deallocated from memory but still exists and can receive messages. This can happen if there is a strong reference to the object that is never released. Zombie objects can cause unpredictable behavior and crashes, so it is important to avoid them.
To detect the Zombie objects we need to enable them in our Xcode project.
Now you will get the crash log in your console.
Here are some tips for avoiding zombie objects in Swift iOS:
- Use weak or unowned references whenever possible.
- Be careful about creating strong references to objects that are passed to functions or closures.
- Avoid creating circular references between objects.
- Enable zombie objects in Xcode to help you track down zombie objects in your code.