Print in Swift 3

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:

Sample Image

"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.

  1. First, with the screen as shown above, I build and run my containing app.

  2. Then, I switch the target to the action extension:

Sample Image


  1. 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:

Sample Image

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.

Sample Image

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



Leave a reply



Submit