简体   繁体   中英

Class properties vs NSArray / NSDictionary

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

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).

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM