如何解决“字符串内插"产生用于可选值的调试描述;否则,将生成调试描述.您是说要明确一点吗?"在Xcode 8.3 beta中? [英] How to solve "String interpolation produces a debug description for an optional value; did you mean to make this explicit?" in Xcode 8.3 beta?

查看:157
本文介绍了如何解决“字符串内插"产生用于可选值的调试描述;否则,将生成调试描述.您是说要明确一点吗?"在Xcode 8.3 beta中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自Beta 8.3起,不计其数的警告字符串内插会为可选值生成调试说明;您的意思是明确显示吗?"出现在我的代码中.

Since beta 8.3, zillions warnings "String interpolation produces a debug description for an optional value; did you mean to make this explicit?" appeared in my code.

例如,在以下情况下弹出警告,其中选项可能会导致无效:

For example, the warning popped in the following situation up, where options could lead to nil:

let msg = "*** Error \(options["taskDescription"]): cannot load \(sUrl) \(error)"

按照先前的设计,对于我(和编译器)来说,可以将可选值插值为"nil"是可以的.但是编译器改变了主意.

As previously designed, it was ok for me (and the compiler) the optionals to be interpolated as 'nil'. But compiler changed its mind.

编译器建议的是添加一个具有以下描述的String构造函数:

What the compiler suggests is to add a String constructor with description as follows:

let msg = "*** Error \(String(describing: options["taskDescription"])): cannot load \(sUrl) \(error)"

显然,在我看来,结果是明确的,但也非常麻烦.有更好的选择吗?我必须修正所有这些警告,还是更好地等待下一个Beta?

Obviously, the results is explicit but also very very cumbersome in my opinion. Is there a better option? Do I have to fix all those warning or better wait for the next beta?

推荐答案

这是在在具有隐式展开的可选内容的情况下.您可以在邮件列表

This is a change that was made in this pull request due to the fact that interpolating Optional(...) into the resultant string is often undesirable, and can be especially surprising in cases with implicitly unwrapped optionals. You can see the full discussion of this change on the mailing list here.

如拉取请求讨论中所述(尽管不幸的是,不是通过Xcode进行的)–使警告静音的一种比使用String(describing:)更好的方法是在要插入的任何内容的可选类型中添加强制类型转换,因此例如:

As mentioned in the pull request discussion (although unfortunately not by Xcode) – one slightly nicer way to silence the warning than the use of String(describing:) is to add a cast to the optional type of whatever you're interpolating, so for example:

var i: Int? = 5
var d: Double? = nil

print("description of i: \(i as Int?)")    // description of i: Optional(5)
print("description of d: \(d as Double?)") // description of d: nil

哪些也可以泛化为as Optional:

print("description of i: \(i as Optional)") // description of i: Optional(5)
print("description of d: \(d as Optional)") // description of d: nil

在Swift 5中,由 SE-0228 ,另一种选择是为appendInterpolation重载> DefaultStringInterpolation :

In Swift 5, with the new string interpolation system introduced by SE-0228, another option is to add a custom appendInterpolation overload for DefaultStringInterpolation:

extension DefaultStringInterpolation {
  mutating func appendInterpolation<T>(optional: T?) {
    appendInterpolation(String(describing: optional))
  }
}

var i: Int? = 5
var d: Double? = nil

print("description of i: \(optional: i)") // description of i: Optional(5)
print("description of d: \(optional: d)") // description of d: nil

而且,如果需要,您甚至可以删除参数标签以完全在模块内(或将其标记为fileprivate的特定文件内)禁用警告:

And, if desired, you could even remove the argument label to disable the warning entirely within a module (or within a particular file if you mark it as fileprivate):

extension DefaultStringInterpolation {
  mutating func appendInterpolation<T>(_ optional: T?) {
    appendInterpolation(String(describing: optional))
  }
}

var i: Int? = 5
var d: Double? = nil

print("description of i: \(i)") // description of i: Optional(5)
print("description of d: \(d)") // description of d: nil

尽管我个人更希望保留参数标签.

Though personally I would prefer to keep the argument label.

这篇关于如何解决“字符串内插"产生用于可选值的调试描述;否则,将生成调试描述.您是说要明确一点吗?"在Xcode 8.3 beta中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆