[swift-users] Any way to declare a method that suppresses the string interpolation warning?

David Sweeris davesweeris at mac.com
Sun Apr 30 15:48:45 CDT 2017


> On Apr 23, 2017, at 23:23, Rick Mann via swift-users <swift-users at swift.org> wrote:
> 
> 
>> On Apr 22, 2017, at 12:23 , Saagar Jha <saagar at saagarjha.com> wrote:
>> 
>> 
>> Saagar Jha
>> 
>>> On Apr 21, 2017, at 04:35, Rick Mann via swift-users <swift-users at swift.org> wrote:
>>> 
>>> I have a debugLog() method that looks like this:
>>> 
>>> func
>>> debugLog<T>(_ inMsg: T, _ inFile : String = #file, _ inLine : Int = #line)
>> 
>> Well, for starters, I don’t see why you need to make this function generic. Why not make inMsg an `Any?`?
> 
> So I can write debugLog(<something other than string>)
> 
>> 
>>> {
>>>    let df = DateFormatter()
>>>    df.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
>>>    let time = df.string(from: Date())
>>>    
>>>    let file = (inFile as NSString).lastPathComponent
>>>    print("\(time) \(file):\(inLine)    \(inMsg)”)
>> 
>> Try \(inMsg ?? “nil”).
> 
> No, this is missing the point. I don't want to have to write this everywhere. I just want to tell the compiler not to issue the warning in these cases, much in the way you can tell the compiler to check printf format specifiers.

IIRC, Swift doesn't include a mechanism for suppressing warnings because we don't want to encourage different "dialects" of the language. The best I can offer for ignoring it is something like:
extension CustomStringConvertible {
  // for "pretty print"
  var pp: String {
    return self.description
  }
}
extension Optional where Wrapped: CustomStringConvertible {
  var pp: String {
    return self?.description ?? "nil"
  }
}
(written on my phone... might contain errors)

That way at least it's quicker to type, cleaner to read, and the code is the same between Optional and non-Optional types.

- Dave Sweeris




More information about the swift-users mailing list