如何解决“字符串插值产生一个可选值的调试描述;你的意思是说清楚吗?"在 Xcode 8.3 测试版中? [英] 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?

查看:21
本文介绍了如何解决“字符串插值产生一个可选值的调试描述;你的意思是说清楚吗?"在 Xcode 8.3 测试版中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 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.

例如,在以下情况下弹出警告,其中options可能导致nil:

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)"

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

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?

推荐答案

这是在 此拉取请求 因为将 Optional(...) 插入到结果字符串中通常是不可取的,而且可能特别令人惊讶 在隐式解包的可选选项的情况下.您可以在邮件列表 这里.

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.

正如在 pull request 讨论中提到的(虽然不幸的是不是 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 重载rel="noreferrer">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 测试版中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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