I want to make a readonly instance of a class in Objective C. I have a vector class which is basically floats for x and y position and some methods. In a lot of cases I need a (0, 0)-vector so I was thinking instead of allocating a new one each time that I would have a shared zero vector, something like this:
// Don't want to do this all the time (allocate new vector)
compare(v, [[Vector alloc] initWithCartesian:0:0]);
// Want to do this instead (use a shared vector, only allocate once)
compare(v, [Vector zeroVector]);
// My attempt so far
+ (Vector *)zeroVector {
static Vector *sharedZeroVector = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedZeroVector = [[self alloc] initWithCartesian:0:0];
});
return sharedZeroVector;
}
// The problem
v.x = 3;
This works fine, except that the zero vector is not readonly, which feels kind of silly. As a note I would like to mention that this is more of a want-to-know-how-to kind of question than an actual problem, I don't know if it will make some actual difference.