Possible Duplicate:
Objective C Equivalent of PHP’s “Variable Variables”
I have created a method in objective C which looks like this:
- (void) startBuildingXArray: (NSMutableArray *) arrayName:(int) senderID;
BUT, here is the tricky part. The particular arrayName argument depends on what the senderID is. Specifically, each arrayName has a number at the end of it (i.e. array1, array2, etc.) that matches the senderID in this method. Is there a way to dynamically add in an integer to the end of array name as a parameter? Or an entirely different and better way to yield the same result?
In other words, what I want to create is this sort of a situation:
- (void) startBuildingXArray: (NSMutableArray *) arrayName+(senderID):(int) senderID;
Of course that is not in any way valid code, I’m just trying to demonstrate what I want to happen. Can anyone suggest any sort of solution?
You’re lucky: Objective-C is a dynamic language, so you can create selectors (method names) at runtime and also add corresponding implementation functions to them. Let’s assume you have already implemented these methods (for example, from
startBuildingXArray:arrayName1:to...arrayName9:etc.), and you’re looking for a way to call them depending on the integer ID:If you want to mass-create methods with names like this, you can use a similar approach but use the
class_addMethod()Objective-C runtime function instead.However, this is a hack. I’d rather suggest you use one function and implement its task depending on the integer identifier that is passed in.