Re: Struct, Object, or Dictionary?
Re: Struct, Object, or Dictionary?
- Subject: Re: Struct, Object, or Dictionary?
- From: James Bucanek <email@hidden>
- Date: Tue, 1 Jul 2008 11:18:48 -0700
Bridger Maxwell <mailto:email@hidden> wrote (Tuesday,
July 1, 2008 10:53 AM -0600):
My second option in this case is to
declare an object that would contain all of those data members as ivars, and
the memory management would be taken care of.
Class
However, it doesn't seem right
to make an object that only has data and no methods. My third option would
be to store all of the data in dictionary pairs. This doesn't seem as
reliable as the last methods though, because there is no guarantee that the
dictionary would contain all the necessary name/value pairs. What is the
"correct" way to do this in Cocoa?
You just answered your own reservation. If the class has any
"business logic" (i.e. default values, ensuring that certain
data value are present, maintaining a valid state, releasing the
objects when done) then the class will most certainly have
methods, even if it's only init and dealloc.
As a general rule, any well defined data entity should be
defined as a class. Especially if there's any chance that
behavior of that data object will need to change at some point
in the future. Good OO programming would encourage you to wrap
all of the member values in accessor methods, which makes it
easy to redefine them in the future. This also allows you to
bind to and observe the values. If you're using Obj-C 2.0 use properties.
Structs are typically used when you need tight control over how
the data is stored/allocated or if you must have a non-object
data blob that you need to pass around by value (e.g. NSRect).
Dictionaries are good for dynamic structures, where you don't
necessarily know or care about what kind or how many values it contains.
--
James Bucanek
_______________________________________________
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