Ale*_*c O 10 printing xcode swift
是否可以扩展Swift功能的功能?我想在我的程序中的每个print()函数上添加一个字符,而不必创建一个全新的函数并重命名print()的每个实例.是否可以创建一个在每个打印实例上附加'*'的扩展名?
这样做的目的是创建一种清除XCODE添加到调试器中的所有额外信息的方法.我使用print语句检查代码的不同部分的进度和成功,但XCODE在几秒钟内填写了数千行超额信息,很快就掩盖了我的具体陈述.
我想做的事:
print("Hello world!")
//Psuedo code:
Extension print(text: String) {
let newText = "*\(text)"
return newText
}
Run Code Online (Sandbox Code Playgroud)
输出:*Hello World!
然后我将过滤星号的Xcode调试输出.我一直在手动这样做
Cod*_*ent 19
您可以print从标准库中掩盖该方法:
public func print(items: Any..., separator: String = " ", terminator: String = "\n") {
let output = items.map { "*\($0)" }.joinWithSeparator(separator)
Swift.print(output, terminator: terminator)
}
Run Code Online (Sandbox Code Playgroud)
由于原始函数位于标准库中,因此其完全限定名称为 Swift.print
小智 18
这段代码在swift 3中为我工作
import Foundation
public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
let output = items.map { "\($0)" }.joined(separator: separator)
Swift.print(output, terminator: terminator)
}
class YourViewController: UIViewController {
}
Run Code Online (Sandbox Code Playgroud)
Pit*_*Pan 14
如果我们想用自定义覆盖所有情况,print我们应该创建新文件,例如:CustomPrint.swift然后粘贴这两个方法:
斯威夫特 5.1
First (according to ThomasHaz answer)
public func print(_ items: String..., filename: String = #file, function : String = #function, line: Int = #line, separator: String = " ", terminator: String = "\n") {
#if DEBUG
let pretty = "\(URL(fileURLWithPath: filename).lastPathComponent) [#\(line)] \(function)\n\t-> "
let output = items.map { "\($0)" }.joined(separator: separator)
Swift.print(pretty+output, terminator: terminator)
#else
Swift.print("RELEASE MODE")
#endif
}
Run Code Online (Sandbox Code Playgroud)
and second because the first one does't cover dictionary and array printing
public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
#if DEBUG
let output = items.map { "\($0)" }.joined(separator: separator)
Swift.print(output, terminator: terminator)
#else
Swift.print("RELEASE MODE")
#endif
}
Run Code Online (Sandbox Code Playgroud)
享受 :)
| 归档时间: |
|
| 查看次数: |
6117 次 |
| 最近记录: |