2

基本上,我有 3 个委托方法,它们具有几乎完全相同的代码,除了每个方法和参数中的 1 行。我意识到我可以封装很多代码以在每个方法中制作 3 或 4 行代码,但我想知道是否有一种方法可以只创建 1 个方法。

此外,被调用tempData的方法与下面的委托方法具有相同的方法名称,但它们实际上是不同的方法。

- (void)addElement:(NSString *)currentElement FromKeyboard:(NSString *)name {

    UIView *tempView; 
    NSMutableArray *tempViewList;
    EquationData *tempData;    


    if ([name isEqualToString:@"leftEqCellKeyboard"]) {
        tempData = leftData;
        tempViewList = leftEqViewList;
        tempView = equationCell.leftView;
    }

    if ([name isEqualToString:@"rightEqCellKeyboard"]) {
        tempData = rightData;
        tempViewList = rightEqViewList;
        tempView = equationCell.rightView;
    }

    [tempData addElement:currentElement]; // different

    if ([tempViewList count] != 0) 
        [self clearViewsStoredIn:tempViewList];
    [self setUpView:tempView fromArray:tempData.equation toArray:tempViewList];
}

- (void)changeState:(NSString *)stateName FromKeyboard:(NSString *)name {

   UIView *tempView; 
    NSMutableArray *tempViewList;
    EquationData *tempData;    

 if ([name isEqualToString:@"leftEqCellKeyboard"]) {
        tempData = leftData;
        tempViewList = leftEqViewList;
        tempView = equationCell.leftView;
    }

    if ([name isEqualToString:@"rightEqCellKeyboard"]) {
        tempData = rightData;
        tempViewList = rightEqViewList;
        tempView = equationCell.rightView;
    }

    [tempData changeState:stateName]; // different

    if ([tempViewList count] != 0) 
        [self clearViewsStoredIn:tempViewList];

       [self setUpView:tempView fromArray:tempData.equation toArray:tempViewList];

}

- (void)changeCharge:(NSString *)chargeIncrease FromKeyboard:(NSString *)name {

    UIView *tempView;
    NSMutableArray *tempViewList;
    EquationData *tempData;   

if ([name isEqualToString:@"leftEqCellKeyboard"]) {
        tempData = leftData;
        tempViewList = leftEqViewList;
        tempView = equationCell.leftView;
    }

    if ([name isEqualToString:@"rightEqCellKeyboard"]) {
        tempData = rightData;
        tempViewList = rightEqViewList;
        tempView = equationCell.rightView;
    }  
    [tempData changeCharge:chargeIncrease]; // different

    if ([tempViewList count] != 0) 
        [self clearViewsStoredIn:tempViewList];

    [self setUpView:tempView fromArray:tempData.equation toArray:tempViewList];


}
4

1 回答 1

0

(也许你应该看看http://codereview.stackexchange.com

“不同”部分始终为 形式[tempData doSomethingOn:aString];,因此您可以使用选择器“doSomethingOn:”进行参数化:

-(void)doSomethingOnString:(NSString*)parameter 
              withSelector:(SEL)selector
              fromKeyboard:(NSString*)name 
{
    // The common parts before

    [tempData performSelector:selector withObject:parameter];

    // The common parts after
}

-(void)addElement:(NSString *)currentElement FromKeyboard:(NSString *)name {
    [self doSomethingOnString:currentElement
                 withSelector:@selector(addElement:)
                 fromKeyboard:name];
}

// etc.

也可以看看:

于 2012-07-21T20:34:51.117 回答