mutableCopy of NSInteger

200 views Asked by At

I'm trying to make a mutableCopy of an NSInteger?. However, since an NSInteger is a non-mutable object this doesn't work. If I don't make a mutableCopy of the row variable the UIPickerView jumps to the next row when selecting one.

Here my code:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == _frequencyPicker) {
        [_detailTableViewController.currentAlarm.cycles replaceObjectAtIndex:0 withObject:[NSNumber numberWithInteger:row]];
        [self.tableView reloadData];
    } else {
        int rowNumber = (int)row;
        // -> mutableCopy
        rowNumber++;
        NSLog(@"%s:%d",__PRETTY_FUNCTION__,rowNumber);
        [_detailTableViewController.currentAlarm.cycles replaceObjectAtIndex: withObject:[NSNumber numberWithInteger:row]];
        [self.tableView reloadData];
    }
}
1

There are 1 answers

5
Sergey Kalinichenko On

Since you are using NSInteger, not NSNumber, you can rewrite your code without declaring and incrementing a primitive variable, like this:

[_detailTableViewController.currentAlarm.cycles
    replaceObjectAtIndex:0
    withObject:@(row+1)];
[self.tableView reloadData];

Since NSInteger is a typedef for a primitive type, mutability considerations do not apply here.