Search code examples
objective-cfunctionvariablespopen

Objective C function variables in popen


So I have this function:

void step (NSTextField *input, char move, int position, NSTextField *label) {
  int delta = input.intValue;
  for (int i = 0; i < delta; i++) {
    pclose(popen("echo move > /dev/tty.usbmodem621", "r"));
    position = position +1;
    [NSThread sleepForTimeInterval:t1];
    NSString *printPosition = [NSString stringWithFormat: @"%i", position];
    label.stringValue = printPosition;
  }

}

And in the line:

    pclose(popen("echo move > /dev/tty.usbmodem621", "r"));

Move should be the variable character that I declared at the beginning. But I can't seem to find out how to do that. Could somebody enlighten me?

Also there is an other thing I don't understand. If for example my input is 20 the and i run this script it counts 20 like it supposed to. However when I input a new value it doesn't ad that one up to the 20 that's already there like I hoped it would. Instead it just displays the new value. Any ideas?

Thanks


Solution

  • To generate string with move:

    NSString *str = [NSString stringWithFormat:@"echo %c > /dev/tty.usbmodem621", move];
    

    About not adding more characters, it is hard to say with this example. I can see that your position always starts from 0 because it is incoming parameter but not retuned back. Maybe that's the issue?