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)")
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
print not working in Swift 3 extensions
It is extremely difficult to retrieve print messages from an extension. The problem is that it's an extension! It isn't running in your app, so it doesn't arrive at your console. Sometimes I find you can solve this problem by switching the debugged process in the Debug Bar at the top of the debug area (at the bottom of the screen, not shown in your screen shot), but at other times this doesn't work.
I'll illustrate a possible technique that seems to be pretty reliable. Look at this screen shot:
"Expand" is an action extension. But my containing app is called "bk2ch13...". So how will I ever manage to pause at the breakpoint shown at the right, which is in the action extension? This is what I do.
First, with the screen as shown above, I build and run my containing app.
Then, I switch the target to the action extension:
- Now I build and run again. But now I am trying to run an action extension, which you can't do, so Xcode asks me what app to run:
So I choose "bk2ch13...". So now we are running my host app again, but we are debugging the extension. So I use my host app to exercise the extension, and sure enough, we pause at the breakpoints and print
statements arrive into the console.
Note, in that screen shot, how the debug bar clearly shows that we are talking to the extension, not the host app.
how to print this kind of pattern using swift 3?
Please check this :
var no = 1
var numberOfRow = 5
for i in 1...numberOfRow {
for _ in 1..<(6-i) {
print(" ", terminator: " ")
}
for j in 1...i {
print("\(j)", terminator: " ")
no = j
}
for k in 1..<no {
no -= 1
print("\(no)", terminator: " ")
}
print(" ")
}
Overriding Swift.print() or sharing function across all modules
Nope, it's not possible, if you want to use a function from a different module, you have to import that module.
The default print
function is part of the Swift
module, which is automatically (implicitly) imported in all Swift files, this is why it's available everywhere without explicitly importing the module.
Swift 3 is there a way to print date as Date not as String
I was able to achieve this using the below code :
let isoDate = "2019-09-21"
let body: [String: AnyObject] = [
"date": isoDate as AnyObject
]
print(body)
This now prints :
["date": 2019-09-21]
print without newline in swift
Starting in Swift 2.0, the recommended method of printing without newline is:
print("Hello", terminator: "")
Related Topics
Textfield Delegate Shouldchangecharactersinrange
Convert Optional String to Int in Swift
How to Open Your App's Settings (Inside the Settings App) with Swift (iOS 11)
Generic Method Override Not Working in Swift
Phase 1 and Phase 2 Initialization in Swift
How to Read File Data Applications Document Directory in Swift
Swift Cannot Assign to Self in a Class Init Method
Use Background Image on Uisearchcontroller iOS 11
Any or a Trouble with Sequence of Functions
Fetching Child Sum from Core Data
How to Use the "Handler" of Uialertaction to Call Another Uialertaction
File Couldn't Be Opened Because You Don't Have Permission to View It Error
Repeating Action Continuously in Swiftui
Fullscreen for Swift Playgrounds on iPad
Swiftui Share Sheet Crashes iPad