|
|
#1 |
|
Calling method with variables?
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.
Code:
NSString *stTmp;
for (i=0; i<[arrPRNint count]; i++) {
stTmp = [arrPRNint objectAtIndex: i];
[prninstall stTmp];
}
|
|
|
|
0
|
|
|
#2 |
|
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.
|
|
|
|
0
|
|
|
#3 |
|
Is prninstall an object or a method here? The syntax for method calls is
Code:
[<object> <method>:<firstvariable> <secondmethodpart>:<secondvariable>]; 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 Code:
[self prninstall:stTmp];
__________________
Sponsor me to cycle 100Km round London in the dark |
|
|
|
0
|
|
|
#4 |
|
Code:
#Import "PrnIntall.h" the way I called one at a time was: Code:
[prninstall printer1]; |
|
|
|
0
|
|
|
#5 |
|
@robbie I've added the general form to the Objective-C tutorial guide.
__________________
If they have to tell you every day they are fair you can bet they arent, if they tell you they are balanced then you should know they are not - Don't Hurt me |
|
|
|
0
|
|
|
#6 | |
|
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: Code:
NSString *stTmp;
for (i=0; i<[arrPRNint count]; i++) {
stTmp = [arrPRNint objectAtIndex: i];
objc_msgSend(prninstall, NSSelectorFromString(stTmp));
}
Quote:
__________________
Sponsor me to cycle 100Km round London in the dark Last edited by yellow; Jun 27, 2008 at 03:19 PM. Reason: Merged contiguous posts. |
||
|
|
0
|
|
|
#7 |
|
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: Code:
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
__________________
15'' Early 2011 MBP | iPhone 5 | iPad (2012) | MacOS X 10.8.3 |
|
|
|
0
|
|
|
#8 |
|
That does it, Ty very very much again
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| Coding style: Where do you defined your local method variables? | NickFalk | iPhone/iPad Programming | 2 | Oct 7, 2011 04:29 AM |
| call method from subclass of uiimageview | sregorcinimod | iPhone/iPad Programming | 3 | Apr 22, 2011 11:54 PM |
| Calling methods | lynkynpark86 | iPhone/iPad Programming | 2 | Feb 20, 2011 01:33 PM |
| Calling method with parameter? | Danneman101 | iPhone/iPad Programming | 8 | Oct 27, 2009 06:31 AM |
| I'd like to call a function with variable/changeable name. | pannini | iPhone/iPad Programming | 7 | Sep 8, 2008 10:46 PM |
All times are GMT -5. The time now is 04:43 AM.








Linear Mode

