A kind person at cplusplus.com advised me that this was a good forum for Objective-C advice. For this reason, this is a cross post of my current position, but hopefully to a more appropriate audience. I am following the CS193P web course on YouTube.
I have some working code that regenerates a dictionary everytime a call is made and to be more efficient I want to create the dictionary as a static thing so that the generation only happens once.
The dictionary being immutable is fine.
This code is for me to learn rather than implement and so although I know the dictionary could be implemented as an array, I want to learn about dictionaries here.
My questions are, is this the right way to declare a static thing like a dictionary or can it only be used with primitives?
If it is not correct, what would be the correct way to do it?
I have done the following in code.
//objectForKey is not recognised by the compiler in this context.
I have some working code that regenerates a dictionary everytime a call is made and to be more efficient I want to create the dictionary as a static thing so that the generation only happens once.
The dictionary being immutable is fine.
This code is for me to learn rather than implement and so although I know the dictionary could be implemented as an array, I want to learn about dictionaries here.
My questions are, is this the right way to declare a static thing like a dictionary or can it only be used with primitives?
If it is not correct, what would be the correct way to do it?
I have done the following in code.
Code:
@implementation HP11Brain
@synthesize programStack = _programStack;
static NSDictionary *dictionaryOfOperators;
+(void) initialize {
if (self == [HP11Brain class])
{
dictionaryOfOperators = [NSDictionary dictionaryWithObjectsAndKeys:
@"/",[NSNumber numberWithInt:1]
, @"*",[NSNumber numberWithInt:2]
, @"-",[NSNumber numberWithInt:3]
, @"+",[NSNumber numberWithInt:4]
, nil];
}
}
// I am trying (within an instance method) to access the dictionary objects with things like
NSString *stringOfOperator = [self.class dictionaryOfOperators objectForKey:operation];
//objectForKey is not recognised by the compiler in this context.
Last edited: