Rya*_*los 8 module swift swift2
我有一个在各种应用程序中使用的日志功能。因为我在整个应用程序中都使用它,所以它也可以方便地进行 Crashlytics 日志记录调用。
但是,并非每个应用程序都使用 Crashlytics。在目标 C 中,您可以使用预处理器条件处理此问题。
如何在代码中处理这个?我认为有一些方法可以使函数成为有条件的。但是我如何选择或弱导入 Crashlytics?
import Foundation
//import Crashlytics
/// Debug Log: Log useful information while automatically hiding after release.
func DLog<T>(message: T, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
// CLSLogv("\(NSString(string: filename).lastPathComponent).\(function) line \(line) $ \(message)", getVaList([]))
print("[\(NSString(string: filename).lastPathComponent) \(function) L\(line)] \(message)")
}
Run Code Online (Sandbox Code Playgroud)
Jav*_*oto 15
您现在可以在 Swift 4.1 中使用新的 canImport() 指令执行此操作。这是描述其工作原理的 Swift Evolution 提案:https : //github.com/apple/swift-evolution/blob/master/proposals/0075-import-test.md
所以你可以这样做:
#if canImport(Crashlytics)
func dLog() {
// use Crashlytics symbols
}
#endif
Run Code Online (Sandbox Code Playgroud)
Ste*_*oyd -1
即使 Swift 编译器不包含预处理器,这在 Swift 中也是可能的。
import Foundation
#ifdef DEBUG
import Crashlytics
#endif
/// Debug Log: Log useful information while automatically hiding after release.
func DLog<T>(message: T, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
#ifdef DEBUG
CLSLogv("\(NSString(string: filename).lastPathComponent).\(function) line \(line) $ \(message)", getVaList([]))
#else
print("[\(NSString(string: filename).lastPathComponent) \(function) L\(line)] \(message)")
#endif
}
Run Code Online (Sandbox Code Playgroud)
现在,以下代码未经测试,可能需要一些调整 - 但如果您想清理代码,它可以提供帮助!顺便说一句,@transparent 将内联代码主体。
import Foundation
#ifdef DEBUG
import Crashlytics
@transparent printfn(item: String) {
CLSLogv(item, getVaList([])
}
#else
let printfn = println
#endif
/// Debug Log: Log useful information while automatically hiding after release.
func DLog<T>(message: T, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
printfn("\(NSString(string: filename).lastPathComponent).\(function) line \(line) $ \(message)")
}
Run Code Online (Sandbox Code Playgroud)
请注意:您必须设置“DEBUG”符号,因为它不是预定义的。在编译器的“Swift 编译器 - 自定义标志”部分的“其他 Swift 标志”行中设置它。您可以使用 -D DEBUG 条目定义 DEBUG 符号。希望我能提供帮助!
| 归档时间: |
|
| 查看次数: |
6295 次 |
| 最近记录: |