Kamil Kamil - 2 months ago 10
Swift Question

Swift - compile program with input parameters

I have the following program in Swift (

myscript.swift
):

print("Enter x: ");
var x = readLine()!
print("x is " + x)

print("Enter y: ");
var y = readLine()!
print("y is " + y)


And I have an input file that contains the following (
input.txt
):

4
6


Usually, I compile using
swift myscript.swift
. However, I want to compile by passing the input file so that I am not prompted for input at the terminal. Is there a way to achieve this?

Answer

I found the solution. The command to compile is as follows:

swift myscript.swift < input.txt

There is no need for additional swift code.