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.
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
Swift: Mkannotation Long Title Text
Dynamically Set Properties from Dictionary<String, Any> in Swift
Can't Set @Ibinspectable Computed Property in UIview
Create Record Only If Parent Exists in Vapor Using Fluent
How to Change an Inout Parameter from Within a Escaping Closure
Custom Markers Disappear on Zoomin The Map and Appear on Zoomout The Map with Clustering
Cast While Looping Over Dictionary in Swift
Error Validating Cms Signature
Parse Nested Completion Handlers
Weird Toolbar with Nested Conditionals Behavior
What Do Detached and Assigncurrentcontext Meaning
Uitextfield with a Placeholder Containing an Image and Text
Reduce Float Precision Using Regexp in Swift
Swiftui Published Updates Not Refreshing
Running Xcode Docc Documentation via Apache Locally on Macos