Calling method with variables?

Discussion in 'Mac Programming' started by trey5498, Jun 27, 2008.

  1. macrumors regular

    Joined:
    Jun 16, 2008
    #1
    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];
    }
    
    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?
     
  2. macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #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.
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    Is prninstall an object or a method here? The syntax for method calls is
    Code:
    [<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

    Code:
    [self prninstall:stTmp];
    
     
  4. thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #4
    Code:
    #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:

    Code:
    [prninstall printer1];
    
     
  5. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    @robbie I've added the general form to the [guide]Objective-C tutorial[/guide] guide.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #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));
    }
    
    Cool :cool:
     
  7. macrumors 6502

    ElectricSheep

    Joined:
    Feb 18, 2004
    Location:
    Wilmington, DE
    #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
    
     
  8. thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #8
    That does it, Ty very very much again ;)
     

Share This Page