简体   繁体   English

NSTask 输出到列表中

[英]NSTask output into a list

I try to list files/folder in a director by using NSTask, how do I get the output in each line into a list?我尝试使用 NSTask 列出目录中的文件/文件夹,如何将每一行中的输出放入列表中? I try print it out and it only display the first line.我尝试打印出来,它只显示第一行。

NSTask * list = [[NSTask alloc] init];
[list setLaunchPath:@"/bin/ls"];
[list setCurrentDirectoryPath:@"/"];

NSPipe * out = [NSPipe pipe];
[list setStandardOutput:out];

[list launch];
[list waitUntilExit];

NSFileHandle * read = [out fileHandleForReading];
NSData * dataRead = [read readDataToEndOfFile];
NSString * stringRead = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding]];
HBLogDebug(@"PATH: %@", stringRead); //Only the first line printed

NSMutableArray *outputlist = [[NSMutableArray alloc] init];

//This part here I'm not so sure how to proceed since it's only one line, I expected multiples line
for (NSString *s in stringRead?dataRead?){
    [outputlist addObject:s];
}

Thanks in advance!提前致谢!

After the comments here is a working example built in Xcode.在此处的评论之后是一个在 Xcode 中构建的工作示例。 The runCommand method is based on this answer updated and with optional use of waitUntilExit so you can see not using it does not freeze the app. runCommand方法基于此答案更新并可选使用waitUntilExit因此您可以看到不使用它不会冻结应用程序。

@implementation AppDelegate

// Arguments:
//    atPath: full pathname of executable
//    arguments: array of arguments to pass, or nil if none
// Return:
//    the standard output, or nil if any error
//
// This method blocks the caller until the called command completes
// (due to both readDataToEndOfFile & waitUntilExit)
// If this an issue either run on a thread or use the asynchronous read alternatives to readDataToEndOfFile
// If there is no need to check terminatation status the waitUntilExit and terminationStatus can be skipped
// by defining RUN_COMMAND_CHECK_TERMINATION as 0

#define RUN_COMMAND_CHECK_TERMINATION 0

+ (NSString *) runCommand:(NSString *)atPath withArguments:(nullable NSArray *)arguments
{
   NSTask *task = [NSTask new];
   NSPipe *pipe = [NSPipe new];

   task.standardOutput = pipe;     // pipe standard output

   task.launchPath = atPath;       // set path
   if(arguments)
      task.arguments = arguments;  // set arguments

   [task launch];                  // execute

   NSData *data = pipe.fileHandleForReading.readDataToEndOfFile; // read standard output

#if RUN_COMMAND_CHECK_TERMINATION
   [task waitUntilExit];           // verify process has exited to prevent terminationStatus faulting
                                   // must do this *after* read otherwise if pipe fills wait can block forever

   if (task.terminationStatus != 0) // check termination status & have output
      return nil;
#endif

   if (data == nil) // check termination status & have output
      return nil;

   return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // return stdout as string
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
   NSString *stringRead =  [AppDelegate runCommand:@"/bin/ls" withArguments:@[@"/"]];
   NSLog(@"PATH: %@", stringRead);
   NSArray *outputList = [stringRead componentsSeparatedByString:@"\n"]; // Note: will produce an empty last line, removing that is left as an exercise
   NSLog(@"Lines: %@", outputList);
}

@end

If your code is freezing either its down to something strange with HBLogDebug (as the above uses NSLog ) or something else in your code which you'll have to ferret out.如果您的代码冻结到HBLogDebug一些奇怪的HBLogDebug (因为上面使用NSLog )或您的代码中的其他东西,您将不得不找出来。

HTH HTH

1.Use NSLog instead of HBLogDebug 1.使用NSLog代替HBLogDebug

`NSLog(@"PATH: %@", stringRead); //it prints all lines in my case`
  1. split the stringRead by newline characters按换行符拆分 stringRead

    for (NSString *s in [stringRead componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]){ [outputlist addObject:s]; }

  2. Better way to list files in objective-c for MACOS在 Objective-c 中为 MACOS 列出文件的更好方法

    [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"<path>" error:nil]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM