Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTimer and logging problems

NSTimer and logging problems
Thread Tools
Mac Elite
Join Date: Mar 2000
Location: Cambridge
Status: Offline
Reply With Quote
Jan 24, 2003, 03:42 PM
 
I'm pretty new to Objective-C and just as new to programming in general, but there it is. I've been working on getting a program to invoke an NSTimer and then log the firings to a file. I've been through Hillegass's chapter on NSTimer time and again, but his implementation is not really the same as mine.

I've got an NSMutableArray to register the firings and the data associated with each firing, but I can't seem to get the selector method right. It doesn't make sense to me and all of the documentation I've run across does not help in the least. If anyone has any ideas or sample code to help me out, I would be appreciative. Thanks.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jan 24, 2003, 04:41 PM
 
I'm not sure I understand your problem. You "can't seem to get the selector method right?" What does that mean, precisely? You're having trouble doing a log in the method called by the timer? You're having trouble typing the selector's name into the timer's initialization method?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Mac Elite
Join Date: Mar 2000
Location: Cambridge
Status: Offline
Reply With Quote
Jan 24, 2003, 05:00 PM
 
In the NSTimer implementation, I need a "selector" and I'm not really sure what that is or how to set it up.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jan 24, 2003, 05:12 PM
 
Originally posted by TimmyDee51:
In the NSTimer implementation, I need a "selector" and I'm not really sure what that is or how to set it up.
You can read all about selectors and how they work in The Objective-C Programming Language. It's included in the documentation with the developer tools, and I strongly recommend it if you want to program in Objective-C.

To answer your question directly, though, a selector can be obtained through the @selector() directive. For example, if your logging method's interface looked like this:
- (void)logIt:(NSTimer *)timer;
the appropriate selector could be obtained with @selector(logIt:).
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jan 24, 2003, 05:21 PM
 
The selector is the name of the method you want to have called when the timer fires. If your selector looks like:
Code:
- (void)myTimerFired: (NSTimer *)timer;
then, you'd pass this selector using:
Code:
@selector(myTimerFired:)
Note the colon at the end of the selector name - this indicates that it takes a single parameter. If (for some non-NSTimer reason), you wanted to pass a selector that took two parameters, you could use:
Code:
- (void)myMethodFirstParameter: (id)parm1 secondParameter: (id)parm2; @selector(myMethodFirstParameter: secondParameter)
ya dig?
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Mac Elite
Join Date: Mar 2000
Location: Cambridge
Status: Offline
Reply With Quote
Jan 24, 2003, 06:02 PM
 
Thanks. I need to think about it some more, but it makes a little more sense. Another question: Do I need to define the logging method separate from the method I implement for the selector?
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jan 24, 2003, 07:34 PM
 
Originally posted by TimmyDee51:
Another question: Do I need to define the logging method separate from the method I implement for the selector?
No. Not as long as the timer's target is the same object that would perform the separate logging method.

Think of a selector this way: It basically represents a method name to the Objective-C runtime. It would be very inefficient to pass a method name as a string, so it uses selectors instead. The @selector() directive tells the compiler to translate the method name inside into the corresponding selector.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Jan 24, 2003, 11:43 PM
 
Think of a selector this way: It basically represents a method name to the Objective-C runtime.
I wish I could be there to see TimmyDee51's face when he realizes just how powerful this mechanism is. I know I love it when lightbulbs turn on for me.

Remember kids, they're messages, not function calls.
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:06 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2