1

I have a method

-(NSData*)getCommonDataWithCommandID:(int)commandID withChannelNumber:(int)channelNo withDataArray:(NSArray*)dataArray withByteArraySize:(int)byteArraySize
{

}

This is called from a lot of places and have a big hierarchy of calling. How can I pass value from dimSliderDecreasedTapped: or tapOnFifthChannel: This is just a portion!

enter image description here

Now I need to add another parameter to this method

bool isFeedbackExpected

But this will violet Open Closed Principle.

What is the best way to do this?

Shuvo Joseph
  • 894
  • 1
  • 12
  • 21
  • I'm not sure I understand your question; I think you know *how* to add another parameter, so are you asking how to do it without disrupting the code base too much? – Droppy Jul 26 '16 at 07:02
  • If i understood your question correctly, you want to add one new parameter to the method which you have implemented in your project and you have called that method from many classes. So there is no magic way to do this. You have to change the method as you want in the declaration and definition and then you can search the method in the project and make change in each class. – Bharat Modi Jul 26 '16 at 07:17

2 Answers2

2

Add another method with that parameter:

- (NSData*)getCommonDataWithCommandID:(int)commandID
                    withChannelNumber:(int)channelNo
                        withDataArray:(NSArray*)dataArray
                    withByteArraySize:(int)byteArraySize
                     feedbackExpected:(BOOL)feedbackExpected {
      ...
}

and call this method from your original method

- (NSData*)getCommonDataWithCommandID:(int)commandID
                    withChannelNumber:(int)channelNo
                        withDataArray:(NSArray*)dataArray
                    withByteArraySize:(int)byteArraySize {
    return [self getCommonDataWithCommandID:commandID
                          withChannelNumber:channelNo
                              withDataArray:dataArray
                          withByteArraySize:byteArraySize
                           feedbackExpected:NO];
}

This will extend the functionality but it will keep the old interface. In Swift this would be even simpler because you could add an optional parameter (a parameter with a default value).

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • How do I pass feedbackExpected value to - (NSData*)getCommonDataWithCommandID:(int)commandID withChannelNumber:(int)channelNo withDataArray:(NSArray*)dataArray withByteArraySize:(int)byteArraySize there are 4 to 5 layer hierarchy. How can I pass feedbackExpected value from those methods? – Shuvo Joseph Jul 26 '16 at 07:13
  • @ShuvoJoseph I don't understand your question. – Sulthan Jul 26 '16 at 07:15
  • I added image to the question. – Shuvo Joseph Jul 26 '16 at 07:35
0

There is two way, first one is to add parameter in existing method and you have to change every where that method is used(called). Second one is, make another method with that extra parameter and call this new method when you required so you not need to change other stuff in project

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75