Bridger Maxwell <mailto:[EMAIL PROTECTED]> 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 (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 [EMAIL PROTECTED]

Reply via email to