Junior — Middle
Kui palju objekte on vaja mälus hoidmise tsükli loomiseks minimaalne arv?
sobes.tech AI
Vastus AI-lt
The minimum number of objects needed to form a retain cycle in memory is two. This occurs when two objects hold strong references to each other, preventing the memory management system from freeing them, even if they are no longer needed.
Swift example:
class A {
var b: B?
}
class B {
var a: A?
}
let objectA = A()
let objectB = B()
objectA.b = objectB
objectB.a = objectA
In this example, objectA and objectB hold each other, creating a retain cycle.