Sté*_*uca 76 optional string-interpolation swift swift3
从beta 8.3开始,zillions警告"字符串插值会为可选值生成调试描述;你的意思是明确说明吗?" 出现在我的代码中.
例如,警告在以下情况下弹出,其中选项可能导致nil:
let msg = "*** Error \(options["taskDescription"]): cannot load \(sUrl) \(error)"
Run Code Online (Sandbox Code Playgroud)
如前所述,我(和编译器)可以将选项插值为'nil'.但是编译器改变了主意.
编译器建议添加一个带有描述的String构造函数,如下所示:
let msg = "*** Error \(String(describing: options["taskDescription"])): cannot load \(sUrl) \(error)"
Run Code Online (Sandbox Code Playgroud)
显然,结果是明确的,但在我看来也非常麻烦.有更好的选择吗?我是否必须修复所有警告或更好等待下一个测试版?
Ham*_*ish 97
这是在此拉取请求中进行的更改,因为插入Optional(...)到结果字符串中的内容通常是不合需要的,并且在隐式展开的选项的情况下尤其令人惊讶.您可以在此处查看邮件列表中对此更改的完整讨论.
正如拉动请求讨论中提到的那样(尽管不幸的是不是Xcode) - 一种比使用静音更明智的方法String(describing:)是将一个强制转换添加到你要插入的任何类型的可选类型中,例如:
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
Run Code Online (Sandbox Code Playgroud)
这也可以推广到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
Run Code Online (Sandbox Code Playgroud)
沉默警告另一种可能的方式是访问appendInterpolation的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
Run Code Online (Sandbox Code Playgroud)
虽然值得注意的是,文档fileprivate不鼓励直接访问Optional(...).
小智 24
两种更简单的方法来处理这个问题.
选项1:
第一个是通过"强制 - 展开" 你希望使用爆炸返回的值(!)
var someValue: Int? = 5
print(someValue!)
Run Code Online (Sandbox Code Playgroud)
输出:
5
Run Code Online (Sandbox Code Playgroud)
选项2:
另一种方式,可能是更好的方式 - 是"安全 - 解包"你想要返回的值.
var someValue: Int? = 5
if let newValue = someValue {
print(newValue)
}
Run Code Online (Sandbox Code Playgroud)
输出:
5
Run Code Online (Sandbox Code Playgroud)
建议选择2.
提示:尽可能避免强行展开(!),因为我们不确定是否总是将值打开.
bri*_*ear 16
似乎使用String(描述:可选)是最简单的.
默认值 ??没有意义的非字符串,例如Int.
如果Int为nil,那么您希望日志显示'nil'而不是默认为另一个Int,例如0.
一些游乐场代码要测试:
var optionalString : String? = nil
var optionalInt : Int? = nil
var description_ = ""
description_ = description_ + "optionalString: \(String(describing: optionalString))\r"
description_ = description_ + " optionalInt: \(String(describing: optionalInt))\r"
print(description_)
Run Code Online (Sandbox Code Playgroud)
产量
optionalString: nil
optionalInt: nil
Run Code Online (Sandbox Code Playgroud)
ano*_*dev 13
在更新到Xcode 8.3并获得大量警告消息后,我想出了以下更像原始输出行为,易于添加,减少了在代码和输出中使用"String(describe :)"的冗长.
基本上,添加一个Optional扩展,它提供一个String来描述可选中的东西,或者只是"nil"(如果没有设置).另外,如果Optional中的东西是String,则将其放在引号中.
extension Optional {
var orNil : String {
if self == nil {
return "nil"
}
if "\(Wrapped.self)" == "String" {
return "\"\(self!)\""
}
return "\(self!)"
}
}
Run Code Online (Sandbox Code Playgroud)
在游乐场使用:
var s : String?
var i : Int?
var d : Double?
var mixed = "s = \(s.orNil) i = \(i.orNil) d = \(d.orNil)" // "s = nil i = nil d = nil"
d = 3
i = 5
s = ""
mixed = "s = \(s.orNil) i = \(i.orNil) d = \(d.orNil)" // "s = "" i = 5 d = 3.0"
s = "Test"
d = nil
mixed = "s = \(s.orNil) i = \(i.orNil) d = \(d.orNil)" // "s = "Test" i = 5 d = nil"
Run Code Online (Sandbox Code Playgroud)
感谢以下链接提供的帮助:
入住如果变量 - 是 - 一 - 可选,和什么类型的-IT-包裹
dar*_*512 11
请参阅Ole Begeman对此的解决方案.我喜欢它.它会创建一个???运算符,然后您可以像这样使用它:
var someValue: Int? = 5
print("The value is \(someValue ??? "unknown")")
// ? "The value is 5"
someValue = nil
print("The value is \(someValue ??? "unknown")")
// ? "The value is unknown"
Run Code Online (Sandbox Code Playgroud)
双击包含此警告的行上显示的黄色三角形.这将向FixIt展示两种解决方案.
用String(describing:)沉默这样的警告:
使用它将成为 String(describing:<Variable>)
例如.:String(describing: employeeName)
提供一个default value以避免此警告:
使用它将成为 (<Variable> ?? default value)
例如.: employeeName ?? “Anonymous” as! String
我的解决方案是制作一个extensionwhich unwrapOptional对象到Any.
当您记录对象或将其打印出来时,您可以看到实际的object或<nil>\xe2\xad\x95\xef\xb8\x8f(文本和视觉字符的组合)。查看起来很有用,尤其是在控制台日志中。
extension Optional {\n var logable: Any {\n switch self {\n case .none:\n return "<nil>|\xe2\xad\x95\xef\xb8\x8f"\n case let .some(value):\n return value\n }\n }\n}\n\n// sample\nvar x: Int?\nprint("Logging optional without warning: \\(x.logable)")\n// \xe2\x86\x92 Logging optional without warning: <nil>|\xe2\xad\x95\xef\xb8\x8f\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
32244 次 |
| 最近记录: |