views:

131

answers:

3

I want a custom class object to hold data and have somehow the feeling that creating about 80 properties is not the best way of doing it.

Most of the properties would be bool values, so i'm thinking about creating Arrays (keys / values) or (probably better) a NSDictionary attached to the object for holding the data. Does that make sense or should i stay with the properties?

Extra: Are there general pros and cons for the different approaches? And what keywords / concepts do i have to search for to find discussions about that somehow general question?

Thanks in advance

+1  A: 

I agree that you are much better off using an object to hold these values. It will make your code much cleaner to read/understand. I would go with using an NSDictionary rather than an array as then you can access each boolean value by name. An array will mean that you have to access the elements by index, making the code much harder to understand.

pheelicks
+2  A: 

You should use classes for creating objects (in future program), if you expect some behavior. I mean, object - is not only something that stores variables. It also supposes some sort of activity (called behavior). And properties are not only the way to set some values (as object's characteristics), but also the way to influence on this behavior (you can add extra code parts, except just saving data - some checks or smth.else).

And if you need only the construction to store data - it's not just better, it's a more logical step to store them as an array or a dictionary.

Dictionary is better in most cases, cause you can access stored values by key names. And your code will be much more understandable. But array should be (I don't know exactly, only suppose) faster to access (it's harder to find string key among other strings than index among sequence of incremented numbers).

kpower
In fact i only need that data object to set and hold data. So when i understand you right i do not need a custom data object for that at all, but should use directly a NSDictionary object instead?
Frank Martin
About usage with 80+ variables... I think, Alex Reynolds' advice (below) is the best one here - you should try to group similar data to create subsets.If your data is really "simple" (as bool, int or float values - with no hard memory-management) you may also use structures (C-structs). Pros: good memory usage (no additional memory for saving keys or something else), code-completion (during typing) and compiler-time checks.
kpower
+1 good answer.
Yar
+2  A: 

An NSArray stores Foundation objects (base NS-type objects like NSNumber, NSString, etc.) in an indexed container. There are no key-value pairs for NSArray. You would use NSDictionary for storing Foundation objects in key-value pairs.

80 of anything is a lot. Can you group these into subsets? That might make maintenance of this data set a bit more manageable.

Alex Reynolds