Vote count:
0
So I have a custom NSObject class called model. My problem is that I have an array of these custom model objects that has already been archived so they can be used in my arrays and nsuserdefaults. I am stuck on how to unarchive them. I have a method that works fine to unarchive individual model objects but I need to know how to unarchive every archived object in the array.
Archive method:
- (void)writeFavoriteObject:(Model *)favorite
{
NSData *favoriteObject = [NSKeyedArchiver archivedDataWithRootObject:favorite];
[[NSUserDefaults standardUserDefaults] setObject:favoriteObject forKey:kNSUSERDEFAULTSCAR];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Unarchive method:
- (Model *)readFavoriteObjectWithKey:(NSString *)key
{
NSData *favoriteobject = [[NSUserDefaults standardUserDefaults]objectForKey:key];
Model *favorite = [NSKeyedUnarchiver unarchiveObjectWithData:favoriteobject];
return favorite;
}
My attempt to unarchive and use my array:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NewsCell";
CarViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
defaultsarray = [[NSMutableArray alloc]init];
defaultsarray = [defaults objectForKey:@"favoritesarray"];
FavoriteCar = [defaultsarray objectAtIndex:indexPath.row];
FavoriteCar = [self readFavoriteObjectWithKey:kNSUSERDEFAULTSCAR];
cell.CarName.text = FavoriteCar.CarModel;
cell.CarImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:FavoriteCar.CarImageURL relativeToURL:[NSURL URLWithString:@"http://ift.tt/1lKyViJ"]]]];
//Accessory
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.layer.borderWidth=1.0f;
cell.layer.borderColor=[UIColor blackColor].CGColor;
cell.CarName.layer.borderWidth=1.0f;
cell.CarName.layer.borderColor=[UIColor whiteColor].CGColor;
return cell;
}
My tableview displays the correct number of rows because I can get an accurate count, but the cell just repeats the first Object. A possible solution would be some kind of method like [FavoriteCar readFavoriteObjectWithKey:kNSUERDEFAULTSCAR]; so each object would be unarchived. Any help would be appreciated!
Objective C unarchive array of custom nsobjects
Aucun commentaire:
Enregistrer un commentaire