I have a time sensitive section of my code which stores, retrieves, and replaces values in an NSMutableDictionary. The problem is that I need to store objects, not primitives, and objects need to be allocated on the heap. However, once there exists an object in the dictionary and I want to replace the value, instead of allocating another number, can't I simply replace the current heap literal, wherever it may be, with the new int?
Problem is, I have been storing NSNumbers and I cannot change the value of an NSNumber because they are immutable.
Currently, I use the @() wrapper operator to create an NSNumber, which I believe must be copied to the heap to be stored in the dictionary.
-(void)setInt: (int)value For: (NSString *)key {
[self.dictionary setValue:@(value) forKey:key];
}
I would imagine that replacing an object's primitive in C would be easy:
-(void)setInt: (int)value For: (NSString *)key {
SomeMutableIntClass * oldValue;
oldValue = (SomeMutableIntClass *) [self.dictionary objectForKey:key];
oldValue.int = value; // Direct copy like a pointer since int is primitive
}
I am wondering if I should just make an Integer class with one int property. Even if I went about making my own class would this actually be faster and require less allocing that my current code?