錯誤報告:
其中已經解決了(FIX致力於爲掌握2017年1月3日),並應因此不再是一個問題,在即將到來的雨燕3.1。
這似乎是一個錯誤(在夫特2.2不存在,只有3.0)與的情況下,相關聯的:
- 使用被迫解開操作者(
!
),用於在一個至少3項表達式(使用至少2個基本運營商進行測試,例如+
或-
)。
- 由於某些原因,鑑於上述情況,Swift會混淆表達式的類型推斷(具體而言,對於表達式中的
x!
術語本身而言)。
對於下面的所有例子,讓:
let a: String? = "a"
let b: String? = "b"
let c: String? = "c"
錯誤存在:
// example 1
a! + b! + c!
/* error: ambiguous reference to member '+' */
// example 2
var d: String = a! + b! + c!
/* error: ambiguous reference to member '+' */
// example 3
var d: String? = a! + b! + c!
/* error: cannot convert value of type 'String'
to specified type 'String?' */
// example 4
var d: String?
d = a! + b! + c!
/* error: cannot assign value of type 'String'
to specified type 'String?' */
// example 5 (not just for type String and '+' operator)
let a: Int? = 1
let b: Int? = 2
let c: Int? = 3
var d: Int? = a! + b! + c!
/* error: cannot convert value of type 'Int'
to specified type 'Int?' */
var e: Int? = a! - b! - c! // same error
錯誤不存在:
/* example 1 */
var d: String? = a! + b!
/* example 2 */
let aa = a!
let bb = b!
let cc = c!
var d: String? = aa + bb + cc
var e: String = aa + bb + cc
/* example 3 */
var d: String? = String(a!) + String(b!) + String(c!)
但是,因爲這是Swift 3.0- dev,我不確定這是否真的是一個「錯誤」,以及什麼是政策w.r.t.在尚未生產的代碼版本中報告「錯誤」,但可能需要爲此提供雷達,以防萬一。
至於回答你的問題,如何來解決這個問題:
- 例如使用中間變量如錯誤不存在:實施例2以上,
- 或明確地告訴Swift在3-術語表達的所有術語都是字符串,如在錯誤不存在:實施例3的上方,
,或者更好的是,使用安全解包您的可選,例如使用可選結合:
var d: String? = nil
if let a = a, b = b, c = c {
d = a + b + c
} /* if any of a, b or c are 'nil', d will remain as 'nil';
otherwise, the concenation of their unwrapped values */
感謝您的回答,我提交了錯誤[這裏](https://bugs.swift.org/瀏覽/ SR-1122) – DMSilva