Skip navigation.
 
mlRe: Subclassing Catch-22
FROM : glenn andreas
DATE : Tue Apr 26 23:57:57 2005

On Apr 26, 2005, at 4:17 PM, Todd Ransom wrote:

> The code looks like this:
>
> - (NSString *)bindingKeyForTableColumnIdentifier: (NSString
> *)identifier {
>
>     NSArray *keys = [NSArray arrayWithObjects: @"name", @"notes",
> @"status", nil];
>     NSArray *identifiers = [NSArray arrayWithObjects: @"Name", @"Notes",
> @"Status", nil];
>
>     NSDictionary *keyForIdentifier = [dictionaryWithObjects: keys
> forKeys: identifiers;
>
>     return [keyForIdentifier valueForKey: identifier];
> }
>
> - (BOOL) tableViewAddColumnWithIdentifier: (NSString *)identifier {
>
>     NSString *bindingKey = [self bindingKeyForTableColumnIdentifier:
> identifier];
>
>     [create a table column, bind it, add it to a table]
>
> }
>
> The theory was that subclasses would override both methods, returning
> the appropriate key to bind to and implementing whatever specifics
> were required for the table column (popup button cells, date
> formatters, etc.).
>
> It all worked fine except I wanted the subclasses to defer to super
> for columns shared by all views.
>



How about taking advantage of some of the flexibility of the underlying
runtime?

@implementation CommonSuperclass
- (BOOL) tableViewAddColumnWithIdentifierName
{
   // whatever name does
}
- (BOOL) tableViewAddColumnWithIdentifierNotes
{
   // whatever notes does
}
- (BOOL) tableViewAddColumnWithIdentifierStatus
{
   // whatever status does
}
- (BOOL) tableViewAddColumnWithIdentifier: (NSString *)identifier
{
   SEL selector =  NSSelectorFromString([NSString stringWithFormat:
@"tableViewAddColumWithIdentifier%@", identifier]);
   NSMethodSignature *signature = [self methodSignatureForSelector:
selector];
   if (!signature) {
       // we don't support this identifier
       return NO;
   }
   NSInvocation *invocation = [NSInvocation
invocationWithMethodSignature: signature];
   [invocation setSelector: selector];
   [invocation invokeWithTarget: self];
   // find out if the routine had any problems by getting it's return
value
   BOOL retval;
   [invocation getReturnValue: &retval];
   return retval;
}
@end

@implementation ASubclass
-  (BOOL) tableViewAddColumnWithIdentifierAddress
{
   // do whatever the special subclass that supports address does
}
@end


You could also use the simpler "performSelector" but since it
technically is for selectors that return an id, it's poor practice to
use it to return a BOOL (plus using an invocation lets you specify
whatever additional parameters you need).





Glenn Andreas                      <email_removed> 
  <http://www.gandreas.com/> oh my!
quadrium | build, mutate, evolve | images, textures, backgrounds, art

Related mailsAuthorDate
mlSubclassing Catch-22 Todd Ransom Apr 26, 20:28
mlRe: Subclassing Catch-22 Marco Scheurer Apr 26, 22:05
mlRe: Subclassing Catch-22 Todd Ransom Apr 26, 22:20
mlRe: Subclassing Catch-22 Ondra Cada Apr 26, 22:32
mlRe: Subclassing Catch-22 Greg Titus Apr 26, 22:57
mlRe: Subclassing Catch-22 Marco Scheurer Apr 26, 23:09
mlRe: Subclassing Catch-22 Todd Ransom Apr 26, 23:17
mlRe: Subclassing Catch-22 Marco Scheurer Apr 26, 23:45
mlRe: Subclassing Catch-22 Ondra Cada Apr 26, 23:54
mlRe: Subclassing Catch-22 glenn andreas Apr 26, 23:57
mlRe: Subclassing Catch-22 Todd Blanchard Apr 27, 00:22
mlRe: Subclassing Catch-22 Todd Ransom Apr 27, 00:30
mlRe: Subclassing Catch-22 Ondra Cada Apr 27, 00:36
mlRe: Subclassing Catch-22 Todd Ransom Apr 27, 00:38
mlRe: Subclassing Catch-22 Marco Scheurer Apr 27, 01:03
mlRe: Subclassing Catch-22 Dan Treiman Apr 27, 01:40
mlRe: Subclassing Catch-22 Ondra Cada Apr 27, 01:47