Unsafe Memory Access in Swift
Memory management in Swift is based on automatic reference counting (ARC), which means that an object exists in memory as long as there is at least one strong reference to it. After that, ARC initiates the object deallocation mechanism, depending on the number of existing weak and unowned object references, the deallocation mechanism will be different.
However, in addition to ARC, Swift also supports manual memory management. In this article, I will tell you what are the ways to work with memory that provide create / read / update / delete (CRUD) operations and much more.
1.Pointers
Manual memory management can be implemented based on pointers. Pointer types vary depending on the need for unsafe memory access.
The data type is:
- Pointers to a piece of memory without an explicit type. Returns the number of bytes. Usually contains
Raw
in the name; - Pointers with an explicit type specified during initialization as a generic parameter. Does not contain
Raw
in the name;
Variability is distinguished by:
- Pointers to a memory area with the possibility of changing it. The name contains
Mutable
; - Pointers to a piece of memory without the possibility of changing it. The name does not contain…