Print Not Working in Swift 3 Extensions

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.

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 to console from application extension

Your most reliable choice is to use NSLog for debugging purposes, but println might actually be working in this case. You just need to attach the Xcode debugger to the extension itself.

In my experience, it's a rather buggy process. This answer has more info on the subject. In short, you need to change the target in the Run drop down to your extension, then after you click run you should get a list of things you can run it in.

Print filename in extension

Assuming that you want to log file name and line number of the caller of that method, this is the only way. Compare Expressions (emphasis added)

When used as the default value of a function or method parameter, the special literal’s value is determined when the default value expression is evaluated at the call site.

In your case:

// Function defined in file "A.swift"
func pinToSuperview(file: String = #file) { print(file) }

// Function called in file "B.swift"
pinToSuperview()

would print "B.swift". On the other hand, #file inside the function body expands to the file name where the function is defined, so

// Function defined in file "A.swift"
func pinToSuperview() { print(#file) }

// Function called in file "B.swift"
pinToSuperview()

would print "A.swift".

print without newline in swift

Starting in Swift 2.0, the recommended method of printing without newline is:

print("Hello", terminator: "")

Swift 2 Iterator Extensions not working in Swift 3

In Swift 3, Sequence has a method first(where:) which behaves very similar to your extension method first(_:).

(From the generated header:)

/// Returns the first element of the sequence that satisfies the given
/// predicate or nil if no such element is found.
///
/// - Parameter predicate: A closure that takes an element of the
/// sequence as its argument and returns a Boolean value indicating
/// whether the element is a match.
/// - Returns: The first match or `nil` if there was no match.
public func first(where predicate: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.Iterator.Element?

Remove the extension and use the first(where:) method in the Standard Library.



Related Topics



Leave a reply



Submit