xcode Swift:扩展 print() 函数的功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39026752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: Extending functionality of print() function
提问by Alec O
Is it possible to extend the functionality of a Swift function? I would like appnd a single character onto every print() function in my program without having to create a brand new function and renaming every instance of print(). Is it possible to create an extension that will append an '*' onto every print instance?
是否可以扩展 Swift 函数的功能?我想在我的程序中的每个 print() 函数上附加一个字符,而不必创建一个全新的函数并重命名 print() 的每个实例。是否可以创建一个扩展,将“*”附加到每个打印实例上?
The purpose of this is to create a way of flushing out all of the extra information that XCODE adds into the debugger. I am using print statements to check on the progress and success of different parts of my code, but XCODE fills in thousands of lines of excess info in seconds that quickly cover up my specific statements.
这样做的目的是创建一种清除 XCODE 添加到调试器中的所有额外信息的方法。我正在使用打印语句来检查我的代码不同部分的进度和成功,但 XCODE 会在几秒钟内填充数千行多余的信息,这些信息很快就会掩盖我的特定语句。
What I want to do:
我想做的事:
print("Hello world!")
//Psuedo code:
Extension print(text: String) {
let newText = "*\(text)"
return newText
}
Output: *Hello World!
输出: *你好世界!
I will then filter the Xcode debugging output for asterisks. I have been doing this manually
然后我将过滤 Xcode 调试输出的星号。我一直在手动执行此操作
回答by Code Different
You can overshadow the print
method from the standard library:
您可以掩盖print
标准库中的方法:
public func print(items: Any..., separator: String = " ", terminator: String = "\n") {
let output = items.map { "*\(import Foundation
public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
let output = items.map { "\(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 { "\(public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
#if DEBUG
let output = items.map { "\(##代码##)" }.joined(separator: separator)
Swift.print(output, terminator: terminator)
#else
Swift.print("RELEASE MODE")
#endif
}
)" }.joined(separator: separator)
Swift.print(pretty+output, terminator: terminator)
#else
Swift.print("RELEASE MODE")
#endif
}
)" }.joined(separator: separator)
Swift.print(output, terminator: terminator)
}
class YourViewController: UIViewController {
}
)" }.joined(separator: separator)
Swift.print(output, terminator: terminator)
}
Since the original function is in the standard library, its fully qualified name is Swift.print
由于原始函数在标准库中,它的全限定名是 Swift.print
回答by Nehrulal Hingwe
This code working for me in swift 3
这段代码在 swift 3 中对我有用
##代码##回答by PiterPan
If we want to cover all cases with custom print
we should create new file for example: CustomPrint.swift
and then paste this two methods:
如果我们想用自定义覆盖所有情况,print
我们应该创建新文件,例如:CustomPrint.swift
然后粘贴这两个方法:
SWIFT 5.1
斯威夫特 5.1
First (according to ThomasHaz answer)
First (according to ThomasHaz answer)
and second because the first one does't cover dictionary and array printing
and second because the first one does't cover dictionary and array printing
Enjoy :)
享受 :)