2015-10-19 86 views
0

我嘗試圍繞一個簡單的記錄器的想法是這樣的:有沒有辦法來防止在Swift中插入字符串?

log(constant: String, _ variable: [String: AnyObject]? = nil) 

這將是這樣使用:

log("Something happened", ["error": error]) 

不過,我想阻止不斷的濫用/變化圖案像下面這樣:

log("Something happened: \(error)") // `error` should be passed in the `variable` argument 

有沒有一種方法,以確保constant沒有用繩子刑警構建通貨膨脹?

+0

'莢CocoaLumberjack' – nhgrif

+0

@nhgrif感謝您的建議。我的問題並沒有真正綁定到日誌上下文,它更多的是關於字符串的純文字和插值以及是否可以區分這些字符。 –

+0

我知道。我只是沉重的,嚴重推薦對另一個日誌庫... – nhgrif

回答

1

你可以使用StaticString代替String

func log(constant: StaticString, _ variable: [String: AnyObject]? = nil) { 
    // You can retrieve `String` from `StaticString` 
    let msg = constant.stringValue 
} 

let foo = 1 
log("test \(foo)") // -> error: cannot invoke 'log' with an argument list of type '(String)' 
相關問題