1
\$\begingroup\$

I have 3 nested NSEnumeration loops, which are used to get the textfields of a custom cell, in a custom table in a custom view in a controller.

How can I make this code more readable and more optimizated?

- (void) textfieldsOperations:(APOperation)op
{
  __block int Valid = 0;
  __block NSArray *sub = _Table.subviews;
  __block MyCell *cell = nil;
  __block UIView *view = nil;
  __block NSMutableArray *ret = [NSMutableArray arrayWithCapacity:5];

  [sub enumerateObjectsUsingBlock:^(id c, NSUInteger index, BOOL *stop)
  {
    if ( [c isKindOfClass:[MyCell class]] ) {
      cell = c;
      [cell.subviews enumerateObjectsUsingBlock:^(id v, NSUInteger index, BOOL *stop)
      {
        if ( [v isKindOfClass:[UIView class]] ) {
          view = v;
          [view.subviews enumerateObjectsUsingBlock:^(id t, NSUInteger index, BOOL *stop)
          {
            if ( [t isKindOfClass:[MyTextField class]] )
            {
              MyTextField *txt = t;
              switch ( op )
              {
                case APOperationClear:
                  // something with "txt"
                  break;
                case APOperationEnableSearch:
                  // something with "valid"
                  break;
                case APOperationGetText:
                  // something with "txt"
                  break;
                case APOperationPos: {
                  // something with "txt"
                  break;
                }
              }
            }
          }];
        }
      }];
    }
  }];
  //[...]      
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

honestly: this code should be replaced completely.

you dont give any informations about how your MyCell and MyTextField look like, so I can just give you a general advice: Let MyTextField have a delegate it can call if a certain operation is triggered on it.

ie it could call textField:(MyTextField *)textField didTriggerOperation:(APOperation)op and the implementation would be similar to:

-(void)textField:(MyTextField *)textField didTriggerOperation:(APOperation)op
{
    if(textField == self.descriptionTextField){
        if(op){
            switch ( op )
            {
              case APOperationClear:
                // something with "txt"
                break;
              case APOperationEnableSearch:
                // something with "valid"
                break;
              case APOperationGetText:
                // something with "txt"
                break;
              case APOperationPos: {
                // something with "txt"
                break;
              }
            }
        }

    }
}

your textField would call is when the operation was triggered like

[self.delegate textField:self didTriggerOperation:operation];
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.