PDA

View Full Version : Calling method with variables?




trey5498
Jun 27, 2008, 02:26 PM
I am trying to call a method that I have done successfully and set up fine. The problem comes in the basic calling with a variable.


NSString *stTmp;
for (i=0; i<[arrPRNint count]; i++) {
stTmp = [arrPRNint objectAtIndex: i];
[prninstall stTmp];
}


obviously this doesnt work, should it be [prninstall "stTmp"]; or how do I let that know it is a variable and that it should read the value and not literally?



Guiyon
Jun 27, 2008, 02:38 PM
Variables in ObjC are not directly accessible via the messaging system. You would have to implement Accessor/Mutator methods in order to gain access unless you are writing code within the implementation of that class. If that is the case, you can directly access it.

robbieduncan
Jun 27, 2008, 02:43 PM
Is prninstall an object or a method here? The syntax for method calls is

[<object> <method>:<firstvariable> <secondmethodpart>:<secondvariable>];


If prninstall is the name of the method you need to supply which object you are messaging. If it is an object you need to supply the method you want to call.

I suspect that prninstall is a method in the current class and you are trying ot message the current object. In that case you want to use


[self prninstall:stTmp];

trey5498
Jun 27, 2008, 02:49 PM
#Import "PrnIntall.h"


the prninstall is the file or i guess in this case the object and the method inside would be a printer like "printer1"

the way I called one at a time was:


[prninstall printer1];

Eraserhead
Jun 27, 2008, 02:51 PM
@robbie I've added the general form to the Objective-C tutorial guide.

robbieduncan
Jun 27, 2008, 02:56 PM
Ah OK so you are trying to use the value of the string as the selector right? So, for example, stTmp is @"printer1" right? If this is the case you have to see the difference between a string you type into the file that the compiler has access to and a NSString object? You cannot directly use a NSString object as a selector.

You can turn a string into a selector with NSSelectorFromString(). Once you have a selector you can use the Obj-C runtime functions to message an object with that selector. The function you need is objc_msgSend

So putting it all together we get something like this:


NSString *stTmp;
for (i=0; i<[arrPRNint count]; i++) {
stTmp = [arrPRNint objectAtIndex: i];
objc_msgSend(prninstall, NSSelectorFromString(stTmp));
}


@robbie I've added the general form to the Objective-C tutorial guide.

Cool :cool:

ElectricSheep
Jun 27, 2008, 03:11 PM
I think I have an idea of what you are trying to do, and what you are looking for is a Core Foundation function called NSSelectorFromString.

Something like this is what you are looking for:


NSString *stTmp;
SEL aMethod;

stTmp = [someArray objectAtIndex: 1]; //assuming this is an array of NSString objects
aMethod = NSSelectorFromString( stTmp ); //set the selector for the string

[prninstall performSelector:aMethod]; //send a message to prninstall to perform the selector

trey5498
Jun 28, 2008, 03:10 AM
That does it, Ty very very much again ;)