print without newline in swift
Starting in Swift 2.0, the recommended method of printing without newline is:
print("Hello", terminator: "")
How do I make a new line in swift
You should be able to use \n
inside a Swift string, and it should work as expected, creating a newline character. You will want to remove the space after the \n
for proper formatting like so:
var example: String = "Hello World \nThis is a new line"
Which, if printed to the console, should become:
Hello World
This is a new line
However, there are some other considerations to make depending on how you will be using this string, such as:
- If you are setting it to a UILabel's text property, make sure that the UILabel's numberOfLines = 0, which allows for infinite lines.
- In some networking use cases, use
\r\n
instead, which is the Windows newline.
Edit: You said you're using a UITextField, but it does not support multiple lines. You must use a UITextView.
How to print an array of characters in swift without a line terminator
In a Playground you need to print one final newline, otherwise the output
is not flushed to the output window:
for chrInStr in strTokenizeMe { print(chrInStr, terminator: " ")}
print()
A (not necessarily better) alternative would be to concatenate
the characters before printing them:
print(strTokenizeMe.map(String.init).joined(separator: " "))
The problem does not occur when running a compiled program, because
the standard output is always flushed on program exit.
In Swift (5), how to I pass an Any... parameter to a print() statement without it printing as an array?
You can simply map your items into strings, join them with the separator and print the resulting string:
public enum Debug {
static func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
#if DEBUG
Swift.print(items.map({String(describing: $0)}).joined(separator: separator), terminator: terminator)
#endif
}
}
Or simply:
Difference between println and print in Swift
In the new swift 2, the println has been renamed to print which as an option "terminator" argument.
(udpated 2015-09-16 with the new terminator: "")
var fruits = ["banana","orange","cherry"]
// #1
for f in fruits{
print(f)
}
// #2
for f in fruits{
print("\(f) ", terminator: "")
}
#1 will print
banana
orange
cherry
#2 will print
banana orange cherry
\n in the end of line with using print
The default terminator for print
is the newline "\n".
You can specify that you do not want any terminator like this:
print("hello", terminator: "")
And since you're in a Playground, open the "Debug Area" to see the print result in the console: the side panel is a preview and doesn't work the same way.
For example, this sequence:
print("hello")
print("hello", terminator: "")
print("hello")
Gives:
hello
hellohello
in the debug area, but will show:
"hello\n"
"hello"
"hello\n"
in the preview panel.
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)"
Related Topics
Why Are Emoji Characters Like 👩👩👧👦 Treated So Strangely in Swift Strings
How to Set Layer Cornerradius For Only Bottom-Left, Bottom-Right, and Top-Left Corner
Getting Keyboard Size from Userinfo in Swift
How to Compare Two Dictionaries in Swift
How to Create Array of Unique Object List in Swift
Nsuserdefaults Not Working on Xcode Beta With Watch Os2
Binary Operator * Cannot Be Applied to Operands of Type Int and Double
Check Password String Strength Criteria in Swift
Choosing Coredata Entities from Form Picker
Swift: How to Use Preprocessor Flags (Like '#If Debug') to Implement API Keys
What Is _: in Swift Telling Me
Detect When a Tab Bar Item Is Pressed
Programmatically Detect Tab Bar or Tabview Height in Swiftui