Class properties vs NSArray / NSDictionary

I want a custom class object to store data and somehow feel that creating about 80 properties is not the best way to do this.

Most of the properties will be bool values, so I'm thinking of creating arrays (keys / values) or (possibly better) NSDictionary attached to an object to store data. Does this make sense or should I stay with the properties?

Additionally: are there common pros and cons for different approaches? And what keywords / concepts do I need to search to find discussions about this somehow general issue?

Thanks in advance

0
source share
3 answers

You should use classes to create objects (in a future program) if you expect any behavior. I mean, an object is not only what stores variables. It also involves some kind of activity (called behavior). And properties are not only a way to set some values ​​(as characteristics of an object), but also a way to influence this behavior (you can add additional parts of the code, except for just saving data - some checks or smth.else).

And if you only need a design for storing data - this is not only better, this is a more logical step for storing them as an array or a dictionary.

A dictionary is better in most cases because you can access stored values ​​by keywords. And your code will be much clearer. But the array should be (I don’t know for sure, just guessing) faster access (it’s harder to find a string key among other strings than an index among a sequence of incremented numbers).

+3
source

An NSArray stores Foundation objects (basic NS types such as NSNumber , NSString , etc.) in an indexed container. There NSArray no key-value pairs for NSArray . You would use NSDictionary to store Foundation objects in key-value pairs.

80 something a lot. Can you group them into subsets? This can make the maintenance of this dataset more manageable.

+2
source

I agree that you are much better off using an object to store these values. This will make your code much cleaner to read / understand. I would like to use NSDictionary, not an array, since then you can access each boolean by name. An array will mean that you need to access the elements by index, making the code much more difficult to understand.

+1
source

Source: https://habr.com/ru/post/1401214/


All Articles