Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As others have answered, it's possible to implement what you want to do in a way you suggested, i.e., by running a <code>while</code> loop inside <code>main</code>.</p> <p>However, <strong>that is not the best way to write a Cocoa app</strong> which reloads a URL once in a few seconds. In a different environment, there's a different standard way to do things. So, you sometimes need to un-learn what you got used to. You might have thought: I want to do <code>X</code>. In language/environment <code>A</code>, I would have coded like <code>P</code> to do <code>X</code>. Now I'd like to use language/environment <code>B</code>. How should I implement <code>P</code>? That's not the way to get used to a new environment. Just ask, How should I do <code>X</code> in the environment <code>B</code>?</p> <p>The most Cocoa-esque way would be this:</p> <ol> <li>Open XCode, create a new project, choose a Cocoa GUI app from the template.</li> <li><p>In the application delegate, implement <code>applicationDidFinishLaunching:</code>. We are going to set up an <code>NSTimer</code>.</p> <pre><code>- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { NSTimer*timer=[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; } </code></pre> <p>This creates a timer which fires once in five seconds, and at each time it fires it calls the method <code>timerFired:</code> of the app delegate itself, specified by <code>self</code>. For more on <code>NSTimer</code>, read <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Timers/Timers.html%23//apple_ref/doc/uid/10000061i" rel="nofollow noreferrer">this apple document</a>.</p></li> <li><p>Implement <code>timerFired:</code>.</p> <pre><code>- (void)timerFired:(NSTimer*)theTimer{ // do whatever you want. you can use plain C to invoke curl, // or if you want you can use Cocoa methods to access a URL. } </code></pre></li> <li><p>There's no fourth step!</p></li> </ol> <p>The <code>main</code> function is provided by the template. It calls <code>NSApplicationMain</code>, which set up the Cocoa system. Eventually, it calls <code>applicationDidFinishLaunching:</code> of your delegate <strong>for you</strong>. You respond to that message. Then you set up a timer. The timer calls the method you specified <strong>for you</strong>. Then you respond to that message, again. That's basically how Cocoa works. The Cocoa system asks you to do something, so you do something. Your control over the flow of the program becomes rather passive, compared to what you would have programmed in Applescript. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload