Swift: how to flush stdout after println?
Maybe the following works (it compiles and builds, but I have not tried it, though):
Place #import <stdio.h>
in your bridging header file
In your code use:
fflush(__stdoutp)
How to grep the stdout of a macOS console app that uses RunLoop
You could try to either flush stdout:
fflush(__stdoutp)
or set the buffer size on stdout to zero (makes it fast, but using more resources):
setbuf(__stdoutp, nil)
See Swift: how to flush stdout after println?
This is typically the solution, when you see output to the terminal, but no output once you pipe the process into another one, like grep. The piping affects the default choice for buffering. Hope this helps!
Why does printf not flush after the call unless a newline is in the format string?
The stdout
stream is line buffered by default, so will only display what's in the buffer after it reaches a newline (or when it's told to). You have a few options to print immediately:
Print to
stderr
instead usingfprintf
(stderr
is unbuffered by default):fprintf(stderr, "I will be printed immediately");
Flush
stdout
whenever you need it to usingfflush
:printf("Buffered, will be flushed");
fflush(stdout); // Will now print everything in the stdout bufferDisable buffering on stdout by using
setbuf
:setbuf(stdout, NULL);
Or use the more flexible
setvbuf
:setvbuf(stdout, NULL, _IONBF, 0);
Swift: Extending functionality of print() function
You can overshadow the print
method from the standard library:
public func print(items: Any..., separator: String = " ", terminator: String = "\n") {
let output = items.map { "*\($0)" }.joined(separator: separator)
Swift.print(output, terminator: terminator)
}
Since the original function is in the standard library, its fully qualified name is Swift.print
How can I output to STDERR with Swift?
For Swift 4.x:
import Darwin
fputs("hello from libc\n", stderr)
print() vs debugPrint() in swift
You use debugPrint when you want more information about what is being printed to the console. The additional information is usually useful for debugging.
print() - Writes the textual representations of the given items into the standard output.
debugPrint() - Writes the textual representations of the given items most suitable for debugging into the standard output.
Basically debugPrint adds additional information that is useful for debugging like type information etc.
An example:
print(1...5)
// Prints "1...5"
debugPrint(1...5)
// Prints "CountableClosedRange(1...5)"
Swift: Print separator not adding a space between array items
The issue is that you explicitly tell the print
statement to put an empty String
as a terminator, hence the issue. If you changed terminator
to " "
, your code would work fine.
Btw it makes no sense to map
the result of print
, which is Void
. I suppose you simply wanted to call forEach
instead.
[4, 6, 7, 12].reversed().forEach{print($0, terminator:" ")}
To actually see the output, you'll also need to call print("")
, since the print buffer is only flushed to the console on newlines and since in the forEach
the terminator is explicitly set to a space, there's no new line added there.
Print in Swift 3
There is almost no functional difference, the comma simply inputs a space either before or after the string.
let name = "John"
// both print "Hello John"
print("Hello", name)
print("Hello \(name)")
Related Topics
How to Replace the Values of Labels in iOS-Charts
Background Upload with Share Extension
Why Use Class Only Protocols in Swift
Difference Between Optional Values in Swift
Swiftui - Unwrap Optional Image Data to Create Image Based on Uiimage(Data)
How to Set iOS 13 Glyphs Programmatically
Appdelegate Segue Alternative Pass Data
How to Select All Text in a Uitextfield Using Swift
Checking Conforms Protocol with Associatedtype in Swift