FROM : Andy Lee
DATE : Tue Jul 01 20:36:52 2008
On Jul 1, 2008, at 1:53 PM, Bridger Maxwell wrote:
> Hey,
> I have been debating using one method over another for a while now,
> and I
> would like to know what the Cocoa way of doing things is. I need to
> group a
> set of data together to use as one entity. In one program I was
> representing
> a puzzle as four strings and a BOOL.
> My first thought would be to represent
> these as a struct. However, the strings within the struct would have
> to be
> memory managed, so I guess that would mean writing my own methods to
> retain,
> release, autorelease, etc. the struct.
I think you mean functions, as in C functions, rather than methods,
unless you mean you would create a class that has methods for
manipulating the struct -- in which case you might as well put the
data into instance variables of the class in the first place, and
there is no need for a struct.
The struct approach is bad because you can't actually do the memory
management right in the general case. For example you can copy a
struct using assignment -- myStructOne = myStruct2 -- and now you have
increased the number of references to the strings inside the structs,
but you haven't retaining those strings an extra time. Similarly if
you pass a struct by value as an argument to a function or method.
> 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. However, it doesn't
> seem right
> to make an object that only has data and no methods.
Even if you use properties and the dot notation, your class would
still have methods for getting and setting its instance variables.
The compiler creates the methods for you and they are implicitly
called when you do something like myObject.myString = @"hello". It is
perfectly okay to have a class whose only methods are accessor methods
(getters and setters for its properties). This is the approach you
should take.
> 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.
There is a time and place for the dictionary approach -- Cocoa does it
in a few places -- but the previous approach is almost always the
right one.
--Andy
DATE : Tue Jul 01 20:36:52 2008
On Jul 1, 2008, at 1:53 PM, Bridger Maxwell wrote:
> Hey,
> I have been debating using one method over another for a while now,
> and I
> would like to know what the Cocoa way of doing things is. I need to
> group a
> set of data together to use as one entity. In one program I was
> representing
> a puzzle as four strings and a BOOL.
> My first thought would be to represent
> these as a struct. However, the strings within the struct would have
> to be
> memory managed, so I guess that would mean writing my own methods to
> retain,
> release, autorelease, etc. the struct.
I think you mean functions, as in C functions, rather than methods,
unless you mean you would create a class that has methods for
manipulating the struct -- in which case you might as well put the
data into instance variables of the class in the first place, and
there is no need for a struct.
The struct approach is bad because you can't actually do the memory
management right in the general case. For example you can copy a
struct using assignment -- myStructOne = myStruct2 -- and now you have
increased the number of references to the strings inside the structs,
but you haven't retaining those strings an extra time. Similarly if
you pass a struct by value as an argument to a function or method.
> 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. However, it doesn't
> seem right
> to make an object that only has data and no methods.
Even if you use properties and the dot notation, your class would
still have methods for getting and setting its instance variables.
The compiler creates the methods for you and they are implicitly
called when you do something like myObject.myString = @"hello". It is
perfectly okay to have a class whose only methods are accessor methods
(getters and setters for its properties). This is the approach you
should take.
> 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.
There is a time and place for the dictionary approach -- Cocoa does it
in a few places -- but the previous approach is almost always the
right one.
--Andy
| Related mails | Author | Date |
|---|---|---|
| Bridger Maxwell | Jul 1, 19:53 | |
| James Bucanek | Jul 1, 20:18 | |
| Keary Suska | Jul 1, 20:30 | |
| Andy Lee | Jul 1, 20:36 | |
| Bridger Maxwell | Jul 1, 20:41 | |
| lbland | Jul 1, 20:45 | |
| Andy Lee | Jul 2, 21:42 | |
| mmalc Crawford | Jul 3, 02:33 |






Cocoa mail archive

