 |
 |
NSTimer and logging problems
|
 |
|
 |
|
Mac Elite
Join Date: Mar 2000
Location: Cambridge
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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. 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|