-1

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];
    }
}
John
  • 8,468
  • 5
  • 36
  • 61

1 Answers1

2

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I get a warning: Incompatible pointer to integer conversion sending 'NSNumber *' to parameter of type 'NSInteger' (aka int) – Javed George Jun 15 '15 at 18:43
  • @JavedGeorge What are you sending to `replaceObjectAtIndex:` parameter instead of zero? – Sergey Kalinichenko Jun 15 '15 at 20:42
  • @JavedGeorge That's very strange: the only line of code in the snippet above that sends any parameters is the first one, and it sends `NSNumber` for the second parameter, which is supposed to be of type `id`, not `NSInteger`. The error must be coming from some other place. – Sergey Kalinichenko Jun 16 '15 at 12:00
  • I think It's because I'm calling [NSNumber numberWithInteger:...] and for the Integer we use a NSNumber literal. I've replaced the whole statement -> [NSNumber numberWithInteger:...] with @(row + 1), now the warning disappeard but when I select a row in the simulator, the problem that the UIPickerView is jumping to the next row is still here. – Javed George Jun 16 '15 at 12:12
  • @JavedGeorge Ah, I see - it should be one or the other, i.e. `[NSNumber numberWithInteger:row+1]` or `@(row+1)`. I like the NSNumber literal syntax, but it's definitely a matter of preference. – Sergey Kalinichenko Jun 16 '15 at 12:30