NSPipe
Instances of NSPipe represent a one-way channel
for communication between two tasks. While one task pours data into
one end of the pipe, another process reads that data out. You can
create a pipe in two ways: with the convenience constructor
pipe or with alloc and
init. Every pipe has a read and a write end that
objects connect to by retrieving NSFileHandle
instances using the methods fileHandleForReading
and fileHandleForWriting.
NSPipe is also
buffered
, which means that it can store
data poured into the write end of the pipe up to a maximum amount
that is defined by the underlying operating system. Example 7-1 shows you how to create a pipe by using
NSPipe and NSTask.
// Instantiate and initialize a new taskNSTask *task = [[NSTask alloc] init];// Create a pipe to communicate with the taskNSPipe *pipe = [[NSPipe alloc] init];// Get a file handle to read from the pipeNSFileHandle *readEnd = [pipe fileHandleForReading];// Set the path to launch the task at[task setLaunchPath:@"/bin/ls"];// Set the arguments; ls takes the directory to list[task setArguments:[NSArray arrayWithObject:@"/"]];// Connect the pipe to the task's stdout[task setStandardOutput:pipe];// Launch the task[task launch];// Once it's launched we can read data from the pipeNSData *stdOutData = [readEnd availableData]; NSLog(@"%s", [stdOutData bytes]);
Distributed Notifications
As noted in Chapter 2, the Foundation framework’s notification ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access