我想要什么:
- 拥有从表中选择的位置查看视频并将其添加到喜爱的数组中。我希望有这个视频,即使关闭应用程序
我尝试过的:
- (IBAction)fav:(id)sender {
if (f==0) {
[favoriteArray addObject:[[stories copy] objectAtIndex:indexArray]];
UIImage *btnImage = [UIImage imageNamed:@"fav_selected.png"];
//[self.favorite setImage:btnImage forState:UIControlStateNormal];
NSMutableArray *url = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"url"]];
NSMutableArray *title = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"title"]];
NSMutableArray *picture = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"picture"]];
[url addObjectsFromArray:[[stories copy] objectAtIndex:indexArray]];
// [title addObjectsFromArray:data];
// [picture addObjectsFromArray:data];
[[NSUserDefaults standardUserDefaults] setObject:url forKey:@"url"];
[[NSUserDefaults standardUserDefaults] setObject:title forKey:@"title"];
[[NSUserDefaults standardUserDefaults] setObject:picture forKey:@"picture"];
//f=1;
}
但是stories是一个带有对象的可变数组,每个对象都是一个有3个键的NSDictionary,在url中,我有这个方法之后stories中的所有对象。
您需要调用:
[[NSUserDefaults standardUserDefaults] synchronize];
将更改写入磁盘。
您似乎每次都在覆盖相同的值。考虑存储一个对象数组,而不是每种类型。例如
NSArray *temp = @[
@{@"url":@"", @"title":@"", @"picture":@""},
@{@"url":@"", @"title":@"", @"picture":@""}
];
[[NSUserDefaults standardUserDefaults] setObject:temp forKey:@"favVideos"];