Nov 08

There are a number of things that Cocoa provides by default that is really great.

If I wanted to call a method after X seconds in Java, I would have to create some sort of Timer class, create an Interface that defines a callback method, execute the timer with some class that implements the interface, have the Timer execute a timer loop handling all the necessary threading code (Thread Pool, etc).

With Cocoa you call performSelector:withObject:afterDelay:

performSelector:withObject:afterDelay:

Invokes a method of the receiver on the current thread using the default mode after a delay.

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

Parameters
aSelector

A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.

See “Selectors” for a description of the SEL type.

anArgument

The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.

delay

The minimum time before which the message is sent. Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.

Discussion

This method sets up a timer to perform the aSelector message on the current thread’s run loop. The timer is configured to run in the default mode (NSDefaultRunLoopMode). When the timer fires, the thread attempts to dequeue the message from the run loop and perform the selector. It succeeds if the run loop is running and in the default mode; otherwise, the timer waits until the run loop is in the default mode.

If you want the message to be dequeued when the run loop is in a mode other than the default mode, use theperformSelector:withObject:afterDelay:inModes: method instead. To ensure that the selector is performed on the main thread, use the performSelectorOnMainThread:withObject:waitUntilDone: orperformSelectorOnMainThread:withObject:waitUntilDone:modes: method instead. To cancel a queued message, use thecancelPreviousPerformRequestsWithTarget: or cancelPreviousPerformRequestsWithTarget:selector:object: method.

This method retains the receiver and the anArgument parameter until after the selector is performed.

Availability
  • Available in Mac OS X v10.0 and later.
See Also
Related Sample Code
Declared In

NSRunLoop.h

So, significant functionality is built into the main system allowing you to call an arbitrary Selector without any extra work.

Tagged with:
Sep 06

Cocoa makes string localization an absolute snap. So easy, in fact, that you should always localize. Just use NSLocalizedString instead of NSString. Always. As Wil Shipley put it:

Let me say this again in slow motion: NEVER type in ANY English string without typing NSLocalizedString() around it! This will save you SO MUCH HASSLE later on when your app is popular. Remember that enterprising polyglots can localize your code from just the binary you ship if you follow a few rules of localization, so you may wake up one day and find that someone from across the world has mailed you a your app in another language. It’s a fuzzy feeling and it gets you instant market-share.

Tagged with:
Aug 09

Matt Gallagher has a good write-up on multi-threaded code and design with Cocoa. He makes good use of NSOperation and NSNotificationCenter for managing communication. One of the key aspects is handover. That is, one specific operation occurs on a single bit of data at a time (that data is not operated on by another thread until the operation is complete). This requires clean design (a good thing) and a clear understanding how a data’s lifetime. While reading the article I was trying to come up with situations where data needed to operated on simultaneously and can’t really think of a good example that a good design would handle (other than complex clustered server architectures).

Tagged with:
Jul 31

There is an interesting piece on Theocacao about “Thinking Like a Cocoa Programmer”. It’s a little self indulgent, but makes quite a few great points. One thing that strike me about the Cocoa developer community that defers from, say, the Java developer community is the focus on the end result and design. That’s over all design (presentation, feel, etc) not just code design.

“The only person you need to impress is the user.”

preload preload preload