Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:03:02+00:00 2026-06-08T07:03:02+00:00

Basically, I have 3 delegate methods that have almost the exact same code except

  • 0

Basically, I have 3 delegate methods that have almost the exact same code except for 1 line in each and the parameters. I realize that I can encapsulate a lot of the code to make 3 or 4 lines of code in each method, but I’m wondering if there’s a way to create just 1 method.

Also, the methods being called on tempData have the same method name as the delegate methods below, but they are actually different methods.

- (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];


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:03:03+00:00Added an answer on June 8, 2026 at 7:03 am

    (Perhaps you should check out http://codereview.stackexchange.com)

    The “different” part is always of the form [tempData doSomethingOn:aString];, so you can parametrize with the selector “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.
    

    See also:

    • -performSelector:withObject:
    • The “Selector” concept in Objective-C
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have a program that filters records from one excel file to another
I basically have 7 select statements that I need to have the results output
I basically have the same question as this guy .. The example in the
I have setup some automatic form submission code. Basically when a form is submitted
I've followed the guide Setting Up Socket Streams and have effectively duplicated that code
So basically I have a protocol inside my interface that I need to include
I basically have three tables, posts, images and postimages (this simply contains the ids
I basically have a unix process running and it is doing some heavy processing
I basically have a server set up and I'm accepting new clients(UNIX) and i'm
I basically have the following string in the format: A,B,C:D,E,F What I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.