You can get delayed execution using a Dispatch timer source.
let timer = DispatchSource.makeTimerSource(queue: .main)
timer.setEventHandler {
… runs every second …
}
timer.schedule(deadline: .now(), repeating: 1.0)
timer.activate()
Reading lines from a file could be easier in Swift. I generally do this using the C standard library. That requires a bit of glue to return lines as Swift strings:
With that in place reading line-by-line is pretty straightforward:
let url: URL = …
guard let f = fopen(url.path, "r") else { fatalError() }
while let l = readLine(f) {
print(l)
}
fclose(f)
Another alternative is to read the entire file into memory and then use an iterator to work through the lines:
let url: URL = …
let fileContents = try! String(contentsOf: url)
let fileLines = fileContents.split(separator: "\n")
var lines = fileLines.makeIterator()
while let l = lines.next() {
print(l)
}