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: Jay Prince <email@hidden>
- Date: Thu, 3 Oct 2002 16:17:28 -0700
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.