I'm just beginning my foray into iOS development and need a code review. I have a custom NSManagedObject
that needs some smart setters. New to obj-c, iOS, and core-data development, I don't trust myself yet. I come from a Java background, so smart setters are what I know, but might not be best when using Core Data. Here is the implementation.
+(void)initialize
{
scoredKeys = @[@"fullSwings", @"putts", @"fairwayBunkers", @"greenBunkers", @"chipsOrPitches", @"rescue", @"wastedOrPenalty"];
}
-(id) initWithHoleNumber:(int)holeNumber
{
self = [super init];
if (self) {
[self setHoleNumber:[NSNumber numberWithInt:holeNumber]];
}
return self;
}
-(void) incrementItem:(NSString *)key
{
[self willAccessValueForKey:key];
NSNumber *value = [self primitiveValueForKey:key];
int intValue = value.intValue;
[self didAccessValueForKey:key];
[self willChangeValueForKey:key];
[self setPrimitiveValue:[NSNumber numberWithInt: intValue++] forKey:key];
[self didChangeValueForKey:key];
[self recalculateScore];
}
-(void) recalculateScore
{
NSString *key;
NSNumber *current;
int tally = 0;
for (key in scoredKeys) {
[self willAccessValueForKey:key];
current = [self primitiveValueForKey:key];
tally = tally + current.intValue;
[self didAccessValueForKey:key];
}
[self willChangeValueForKey:@"score"];
[self setPrimitiveValue:[NSNumber numberWithInt:tally] forKey:@"score"];
[self didChangeValueForKey:@"score"];
}
I am also seeking general comments about my approach.
- In the core-data docs, it says the auto generated accessors are much faster than anything a developer can write, so is this a good idea?
- Should I not even have a
score
property, and just have a method that returns the tally?
initWithEntity:insertIntoManagedObjectContext:
– ikinci viking Jan 17 at 16:49