Re: Class Message
Re: Class Message
- Subject: Re: Class Message
- From: Quincey Morris <email@hidden>
- Date: Mon, 29 Mar 2010 03:05:21 -0700
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 (email@hidden)
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:
This email sent to email@hidden