Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

tldr;

Before:

    let process = Process()
    process.executableURL = URL(fileURLWithPath: "/bin/ls")

    let pipe = Pipe()
    process.standardOutput = pipe

    try! process.run()
    process.waitUntilExit()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    if let output = String(data: data, encoding: .utf8) {
        print(output)
    }
After:

    let result = try await run(
        .name("ls"),
        arguments: ["-1"],
        output: .string(limit: 1 << 20)
    )
    print(result.standardOutput ?? "")


Why bother with invoking shell programs instead of using built-in ways of the language to do the same task?

  let result = try! FileManager.default.contentsOfDirectory(atPath: "/etc")
  print(result)
Various other FileManager methods are available:

https://developer.apple.com/documentation/foundation/fileman...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: