0

In my Mac OS app I have NSTask calling a Python script, which then returns "connected" via NSPipe. I then read the data in my Obj-C class, and put it in a string:

NSMutableData *data = [[NSMutableData alloc] init];
NSData *readData;

while ((readData = [readHandle availableData])
       && [readData length]) {
    [data appendData: readData];
}

NSString *aString;
aString = [[NSString alloc]
                  initWithData: data
                  encoding: NSASCIIStringEncoding];

NSLog(@"append%@me",aString);

Later when I try to concatenate the output with another string, I can't - it prints on another line:

appendconnected
me

And also, I can't test the string with:

if ([string isEqualToString:@"connected"]) {
    NSLog(@"yes");
} else {
    NSLog(@"no");
}

it shows that they are not equal, although they are!

Why is it?

janeh
  • 3,734
  • 7
  • 26
  • 43

1 Answers1

0

There be a newline in the stream.

How are you printing the data on the python side?

If you are using print "connected", it'll output a newline, too.

Note that not outputting a newline may cause the data to not be read until the pipe is closed (or more data is written) as the system may buffer things for a bit.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • You're right, I tested it with: `if ([strippedString isEqualToString:@"connected\n"])` , it is there. I'll remove it. Sounds like there is no good reason to NOT send the /n from Python. (unless I could "force" close the pipe?) – janeh Jul 30 '12 at 17:40