Junior — Middle
Kus paikneb struktuur, kui selle sees on välja, mis on klassi näide?
sobes.tech AI
Vastus AI-lt
In iOS, if a struct contains a field that is an instance of a class, the struct itself is placed on the stack, while the class field is on the heap.
This is because structs are value types, stored directly where they are declared (usually on the stack), whereas classes are reference types, with their objects stored on the heap, and the struct only holds a reference to that object.
Example:
class MyClass {
var value: Int
init(value: Int) {
self.value = value
}
}
struct MyStruct {
var obj: MyClass
}
var s = MyStruct(obj: MyClass(value: 10))
Here, s is a struct located on the stack, and the MyClass(value: 10) object is on the heap, with s.obj holding a reference to that object.