views:

142

answers:

2

I have an app where I need one table of information with the following fields:

field 1 - int or char
field 2 - string (max 10 char)
field 3 - string (max 20 char)
field 4 - float

I need the program to filter on field 1 based upon a segmented control and select a field 2 from a picker. From this data I need to look up field 4 to use in a calculation. Total records will be about 200. I never see it going above 400 - 500. I am going to use a singleton which I am able to code, I just need help with the structure for this data persistence.

What type of data structure should I use for this? Should I use NSNumber, NSString, etc. or old data types like float, Char, etc? I thought about a struct put into an array but there is probably a better way. This is new to me so any help or reference to examples would be great. I also thought about a plist or dictionary but it looks like it is just a lookup and a field which obviously won't work. Core data looked like overkill to me. Also, any recommendations regarding how I get initial data into it? I want the user to be able to edit and add to the database.

Sorry for the old terms, you can see what generation I am from... Thanks in advance!!!!

A: 

I would think a struct would work fine for what you want to do. I'm assuming for any particular record fields 1-4 pertain to the same record. In which case, you can use a struct to represent the record.

Now, it seems you are having trouble figuring out how to efficiently index these records. I'd suggest storing the records themselves in a list or vector. Actually, if you think you are going to be doing a lot of deleting, a list would probably be best. This way you can index directly to the element you want to delete and then delete in constant time. You can then create indexes to the elements in the list or vector using a couple different data structures. You could use a map or an array to map directly based on the integer index field 1.

Then, you can use a map or hashmap that maps by field 2.... this will then give you a couple of different ways of indexing the records.

Remember to only store references to the structs you are creating... so you aren't duplicating the records all over the place.

Creating the wrapper singleton around these data structures shouldn't be too hard. The hardest part would be making sure that you are deleting/inserting everything correctly, beyond that though it should work fairly well.

Polaris878
+1  A: 

Core Data isn't really "overkill" - this is exactly the kind of thing it's designed to do for you. You might be used to "databases" being expensive and heavyweight, but Core Data is built on sqlite, which is super tiny and fast. Trust me, it won't be a problem for your case.

apenwarr