Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 27, 2008, 02:26 PM   #1
trey5498
macrumors regular
 
Join Date: Jun 2008
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];
}
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?
trey5498 is offline   0 Reply With Quote
Old Jun 27, 2008, 02:38 PM   #2
Guiyon
macrumors 6502a
 
Join Date: Mar 2008
Location: North Shore, MA
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.
Guiyon is offline   0 Reply With Quote
Old Jun 27, 2008, 02:43 PM   #3
robbieduncan
Demi-God (Moderator)
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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];
robbieduncan is offline   0 Reply With Quote
Old Jun 27, 2008, 02:49 PM   #4
trey5498
Thread Starter
macrumors regular
 
Join Date: Jun 2008
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];
trey5498 is offline   0 Reply With Quote
Old Jun 27, 2008, 02:51 PM   #5
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
@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
Eraserhead is offline   0 Reply With Quote
Old Jun 27, 2008, 02:56 PM   #6
robbieduncan
Demi-God (Moderator)
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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:
Originally Posted by Eraserhead View Post
@robbie I've added the general form to the Objective-C tutorial guide.
Cool

Last edited by yellow; Jun 27, 2008 at 03:19 PM. Reason: Merged contiguous posts.
robbieduncan is offline   0 Reply With Quote
Old Jun 27, 2008, 03:11 PM   #7
ElectricSheep
macrumors 6502
 
Join Date: Feb 2004
Location: Wilmington, DE
Send a message via AIM to ElectricSheep
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
ElectricSheep is offline   0 Reply With Quote
Old Jun 28, 2008, 03:10 AM   #8
trey5498
Thread Starter
macrumors regular
 
Join Date: Jun 2008
That does it, Ty very very much again
trey5498 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC