Re: How to organize an app (baffled with Accessor methods)
Re: How to organize an app (baffled with Accessor methods)
- Subject: Re: How to organize an app (baffled with Accessor methods)
- From: Jonathan Jackel <email@hidden>
- Date: Thu, 03 Oct 2002 23:01:46 -0400
If you use accessors, archiving data is pretty darn easy. And you're
ignoring NSUserDefaults. Sure, you can design a program the way you
describe, but I would guess that it probably takes longer than writing a few
accessors, and your code isn't nearly as maintainable.
There are a number of tools out there that will save you from accessor
drudgery. I'm partial to AccessorMacros.h in MPWFoundation, but there are
other ways to deal with it.
Jonathan
on 10/3/02 7:17 PM, Jay Prince at email@hidden wrote:
>
On Wednesday, October 2, 2002, at 09:06 pm, Jason Galarneau wrote:
>
> If you can imagine what I'm trying to do, I would be most grateful for
>
> any advice or sample code.
>
>
People tell you to create classes for your data structures-- and the
>
examples do this-- and then implement accessors for all the stuff.
>
>
I didn't want to write a bunch of accessors that didn't do much of
>
anything, so I looked at "takeValue:forKey" and its partner. I was
>
worried about memory management but it turns out that key-value coding
>
does correct retains and releases when you update member data.
>
>
So you could go that route and not write any custom accessors unless
>
you need custom logic.
>
>
But I didn't go that route. I did the next thing which was to not have
>
any custom data classes at all, and to do everything with Arrays and
>
Dictionaries. Then its really easy to save these in plist format.
>
Unless you have some custome classes you need to store (for instance,
>
plist supports NSDate but not NSCalendarDate, so I have to account for
>
that-- actually just used NSNumber instead and created a calendar date
>
from that using initwithtimeintervalesincereferencedate.
>
>
So, when I have a series of some data type, its an NSArray of
>
dictionaries. Instead of classes with instance variables, I have
>
dictionaries and use objectForKey to get the right one.
>
>
This also plugs right into TableViews-- you just get the array index
>
(rownum) and use the table views identifier as the key to get the
>
object out of the dictionary.
>
>
This is working great for me. It also has an advantage-- if you create
>
a plist with default settings, whenever you create a new entity, you
>
just load that dictionary in, and merge it with the values the user
>
actually set, and you always have an entity with reasonable values.
>
>
What I mean is, I have a preference pane that has controls on it, and
>
those controls Action methods put values in a dictionary. I get the
>
default dictionary for the entity type and replace all the objects
>
where they made changes and where they didn't the defaults remain.
>
Thus you can put up a window with settings and even if they don't
>
change anything (causing the action method not to be called for a
>
particular setting) you end up with a fully formed dictionary.
>
>
Hope that's not confusing.
>
>
Have fun.
>
>
Jay
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.