On Mar 29, 2010, at 01:48, Kevin Bracey wrote:

> I have a simple class whose job it is to check if it's a first run and create 
> all the Folders required such as App Support and Logs, check CFBundleVersion 
> and if different from the last run update any App Support files and reset any 
> UserDefaults. That sort of thing.
> I know how to do this, but I'm not sure of the best/simplest pattern for 
> Cocoa.
> 
> My Class has only 1 message and stores no instance data, all the states are 
> in the UserDefaults. Trash the Preferences .plist and you get a nice fresh 
> firstrun.
> 
> I currently have ad-hock code in the AppDelegate, duplicated functionality in 
> quiet a few apps and want to standardise on one way, cutting down on the code 
> I need to maintain

Think about it this way: a class method is a lot like a global function, except 
that it lives in a class-specific name space. (This gives it a slight advantage 
over a real global function, which pollutes the global namespace.) So, you can 
create a "global function" method like this:

        + (BOOL) validateFirstRun: (NSError**) outError;

(If there are no parameters in the method signature other than the returned 
error, it's not usual to put "Error" in the method name.)

You don't need to advertise outside of the class how this method is implemented 
-- no outside clients care whether there's any class instance involved. Your 
app delegate simply invokes [FirstRun validateFirstRun: &error] and deals with 
the return error if any.

Within the class, you can take two approaches:

1. Do everything you need with class methods, using file-local static variables 
for storage. You don't ever need to create an instance of the class. This works 
pretty well in most cases.

2. Sometimes, not having instance variables turns out to be awkward. In that 
case, you can have your public class method create an instance of the class, 
and put all the work in instance methods.

Or you can use #2 if you just prefer to code with instance methods -- there's 
no real downside.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to