0

Im programming an app that gets names and id's from a database and I want to make it populate the table view with names and when you click the name it goes to a posts the id of the item clicked. My question is what data structure should i use to store the id and name. Would it be smart to use a Dictionary with a the id as the key and the name as the value?

Database response looks like this

[{"id":1,"name":"Name of Business 1"},{"id":2,"name":"Name of Business 2"}]

user3882976
  • 105
  • 10

2 Answers2

0

For backend - Use SQLite or read up on Core Data. A dictionary isn't going to persist, isn't the best for large data, and won't allow duplicate keys (names in your case).

Adding core data to my iPhone app

If you're just talking about what to use to hold data you just got from a database, then a dictionary will work fine, but be sure your keys are unique, to prevent collisions.

Community
  • 1
  • 1
Parker
  • 8,539
  • 10
  • 69
  • 98
0

It depends on how much you want to do with the data, there are different approaches

  1. Creating a proper class to hold the data

    @interface

    @property (copy, nonatomic) NSString *id;

    @property (copy, nonatomic) NSString *name;

    @end

  2. Using an NSArray of NSDictionary. Disadvantage is that all the keys are all hardcoded, so heavy manipulation is error-prone.

  3. Using NSDictionary that maps from id to name. This is the least flexible one, since it relies on the fact that you have only one field apart from the id field. id also has to be unique, but I guess that's the case for you.

Khanh Nguyen
  • 11,112
  • 10
  • 52
  • 65