anoyd_bi_me_grammers anoyd_bi_me_grammers - 7 months ago 16
Swift Question

Are properties in a Swift class automatically references?

I have this class in which the properties are the class itself.

class Voxel {
var vertices : [Vertex] = []
var above : Voxel?
var below : Voxel?
var front : Voxel?
var back : Voxel?
var left : Voxel?
var right : Voxel?

It acts as a node in a Doubly 3D Linked List, but the thing is, in C and C++, I would have to store the neighbors as pointers (which I want to do to the vertex array as well).

Is this automatically done for me in Swift?

I don't see a fine way of holding an array of vertices as an array of pointers, and I certainly don't want to hold the actual values, as I'm going to have thousands of voxels.

Rob Rob

Because Voxel is a class, a reference type, these are pointers. If it were a struct, it would be a value type and therefore each new reference would be a new copy.

By the way, be wary of strong reference cycles. Make sure one instance's above is not another instance whose below refers back to the other instance. To resolve this, you'd make all of those references weak and then have some collection (e.g., a set or maybe an array or dictionary) which maintains strong references to all of the instances in the doubly linked list.