Storage Modifiers
On the Cocoa platform, which uses ARC rather than Garbage Collection for memory management, three Storage Modifier keywords are available to control how object references are stored in local variables, fields or properties.
By default, all variables and fields are strong
– that means when an object is
stored in the variable, its retain count is increased, and when a variable's value gets overwritten, the retain count of the previously stored object gets reduced by one.
Storage modifiers can be used on type names in:
- local variable and field declarations,
- property declarations,
- method parameter declarations.
The strong
, weak
and unretained
storage modifiers are available in all languages:
Oxygene | C# | Swift | Java |
---|---|---|---|
strong |
__strong |
strong |
__strong |
weak |
__weak |
weak |
__weak |
unretained |
__unretained |
unretained |
__unretained |
These modifiers are only available for the Cocoa platform and cannot be used in .NET and Java projects unless Cross-Platform Compatibility is enabled, in which case they are ignored on .NET and Java.
Strong
strong storage is implied as default whenever referencing types without any of the other two storage modifiers. The following two variable or field declarations therefore are the same:
var name: NSString;
var name: strong NSString;
NSString name;
__strong NSString name;
var name: NSString?
strong var name: NSString?
NSString name;
__strong NSString name;
Weak
Optionally, weak references store the object reference without affecting the retain count. In addition, weak references also keep track of the referenced object and automatically will get set to nil
/null
when said object is released — without any interaction from your own code. This makes weak references ideal to hold on to an object "for as long as it stays around", without having to worry about stale object pointers.
The most common use for weak storage is to avoid Retain Cycles, where one object holds on to another and the second object also references the first.
var name: weak NSString;
__weak NSString name;
weak var name: NSString?
__weak NSString name;
Unretained
A third type of object references are so-called unretained
references. These behave like regular object pointers in old-school languages: they store the object address and do not keep track of the objects life cycle.
When the object gets released at a later point in time, an unretained reference will point to stale memory. For this reason, unretained is the most seldom used storage modifier, and should only be used in controlled scenarios when your code has exact control or knowledge about the life cycle of the referenced objects. The upside of unretained is that it has an ever so slight performance benefit over weak.
var name: unretained NSString;
__unretained NSString name;
unretained var name: NSString?
__unretained NSString name;
See Also
- Automatic Reference Counting
- Retain Cycles
- Storage Modifiers (Oxygene)
- Storage Modifiers (C#)
- Storage Modifiers (Java)