I may be trying to abuse the preprocessor. I want to see if what I have in mind is even possible.
My class has @properties that all have the same bodies. I want to generate these bodies with a preprocessor macro. E.g.:
- (float) accelerometerSensitivity {
return [dict floatForSelector:_cmd or:1];
}
- (void) setAccelerometerSensitivity:(float) n {
[dict setFloat:n forSelector:_cmd];
[dict writeToFile:[self globalDataFilename] atomically:YES];
}
- (float) returnSpringTension {
return [dict floatForSelector:_cmd or:0];
}
- (void) setReturnSpringTension:(float) n {
[dict setFloat:n forSelector:_cmd];
[dict writeToFile:[self globalDataFilename] atomically:YES];
}
// set*ForSelector methods are in a category on NSMutableDictionary and depend on a function that translates selectors into strings:
// NSString* keyFromSelector(SEL selector);
The idea is that instead of using string literals (or string constants) as keys into the dictionary, I derive the string from the selector name. This way I am sure that the spelling of the key matches the property name and essentially get the benefit of compile-time validation of dictionary keys.
What I want to do is say something like SELECTOR_PROPERY(accelerometerSensitivity) and have it expand into the the getter and the setter. The main difficulty I have in implementing this as a pre-processor macro is generating the setter name from the property name. I need to uppercase the first letter of the property name, and I have no idea how to do that in the preprocessor.