我目前有一個函數寫成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);
}