我目前有一个函数写成saveWorkout,它将一个NSMutableArray从一个Singleton类中保存到另一个NSMutableArray。这个函数是第一次运行,但是当我第二次运行它时,它擦除之前存储在元素0中的内容,并将其替换为新数组(当用户单击表格时收集的字符串集合) 。NSMutableArray覆盖以前存储的元素
这里是我的功能:
-(IBAction)saveWorkout{
WorkoutManager *workoutManager = [WorkoutManager sharedInstance];
[[workoutManager workouts] insertObject: customWorkout atIndex: 0];
NSLog(@"%@", [workoutManager workouts]);
}
customWorkout是什么initialially创建NSMutableArray的(基于用户点击了什么)。因此,如果我的第一个数组由blah1,blah2组成,那么这两个值将存储在训练数组中。然而,如果我然后点击blah2,blah 3,训练数组将有两个标识数组(blah2,blah3),并且它不保留第一个数组。任何想法为什么发生这种情况?
这是我如何形成customWorkout:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
[customWorkout insertObject:str atIndex:0];
//Test Code: Prints Array
NSLog(@"%@", customWorkout);
}