I've got several different classes in my code utilising identical methods -- resulting in a lot of duplicated lines -- and I recently found out about adding Categories which promises an effective solution to the problem. To give one of the smaller examples, my previous methods were (typically) called in the traditional way like this:
if((thisNum=[self valueInTextField:ctr]) != 0)
//... do stuff here...
- (int)valueInTextField:(int)tagNum
{
NSTextField *field = [[prizeWindow contentView] viewWithTag:tagNum];
int value = [field intValue];
return value;
}
I deleted the above method and added the Category:
@implementation NSTextField(GetFieldValue)
- (int)valueInTextField
{
NSTextField *field = [[[self window] contentView] viewWithTag:tagNum]; // DOESN'T LIKE THIS!!
return [self intValue];
}
@end
However, it doesn't like me asking it to go find the textField itself using [[self window] contentView], so the only way I can get it to work is to (obviously) delete the offending line and pass something like:
if([[[[self window]contentView]viewWithTag:ctr] valueInTextField] != 0)
I'm sure you can see what I'm trying to achieve here. Is there any way I can get a Category to identify the required field as hinted at above -- i.e. without physically passing it myself? Thanks in advance :-)