2

基本的に、それぞれ1行とパラメーターを除いて、ほぼ同じコードを持つ3つのデリゲートメソッドがあります。多くのコードをカプセル化して、各メソッドで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をチェックする必要があります)

「異なる」部分は常に形式であるため、セレクター「doSomethingOn:」[tempData doSomethingOn:aString];を使用してパラメーター化できます。

-(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 に答える