Segev
Segev

Reputation: 19303

Array or dictionary for saving a few instances?

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.carMakes = [[NSArray alloc]
                     initWithObjects:@"Chevy",
                     @"BMW",
                     @"Toyota",
                     @"Volvo",
                     @"Smart", nil];

    self.carModels = [[NSArray alloc]
                      initWithObjects:@"Volt",
                      @"Mini",
                      @"Venza",
                      @"S60",
                      @"Fortwo", nil];

    self.carImages = [[NSArray alloc]
          initWithObjects:@"chevy_volt.jpg",
                      @"mini_clubman.jpg",
                      @"toyota_venza.jpg",
                      @"volvo_s60.jpg",
                      @"smart_fortwo.jpg", nil];
}

The above code is populated in a Table view. I want to give the user the option to choose a few cars and by doing so the name of the car, model and picture will be saved in another list. I don't know where I should save the data the user chooses and how. I want to be able to show the user chosen car list on a TableView later on.

Upvotes: 1

Views: 207

Answers (2)

iDev
iDev

Reputation: 23278

You can use array or dictionary for this. But a better approach is to create an array of model classes to hold these data. This is best suited for MVC architecture. For example, you can create a model class as Car which has properties, NSString *make, NSString *model and UIImage *image declared in it. If you want to populate the table view you need to create an array of these car model objects and use that in delegate methods to populate the cells. If you want to persist the data, you can either convert this into a dictionary form and then store it as a plist. For more details, check this tutorial from ray wenderlich

A typical model object will look like,

@interface Car : NSObject {}
@property (nonatomic, retain) NSString *make; 
@property (nonatomic, retain) NSString *model;
@property (nonatomic, retain) UIImage *image;
@end

Create array of model objects as,

Car *carObject = [[Car alloc] init];
carObject.make = @"Chevy";
//set other properties and create more objects
NSMutableArray *carList = [[NSMutableArray alloc] init];
[carList addObject:carObject]; //you can do this in a loop

While displaying you can just use [carList objectAtIndex:indexPath.row].make etc..

or

Car *myCar = [carList objectAtIndex:indexPath.row];
cell.textLabel.text = myCar.model;

When the user chooses some cars, save those model objects into a separate array and save it as plist.

Upvotes: 2

Paramasivan Samuttiram
Paramasivan Samuttiram

Reputation: 3738

Suppose, you added the following cars into an array as follows.

[arraySelectedCars addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"BMW", @"Makes", @"Mini", @"Model", @"chevy_volt.jpg", @"Image", nil]];
[arraySelectedCars addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Toyota", @"Makes", @"Mini", @"Venza", @"toyota_venza.jpg", @"Image", nil]];

You can save them into NSUserDefaults, which reside into user's device till user delete apps or until you remove it.

[[NSUserDefaults standardUserDefaults] setValue:arraySelectedCars forKey:@"SelectedCars"];
[[NSUserDefaults standardUserDefaults] synchronize];

Later you can access the array and reload tableview as follows.

NSMutableArray *arraySelectedCars = [[NSUserDefaults standardUserDefaults] valueForKey:@"SelectedCars"];

Upvotes: 2

Related Questions