我有一个快速的代码
if(pendingComboOperation == nil){
if(accumulator != nil){
let descPart2 = symbol + String(accumulator!)
}
else {
let descPart2 = ""
}
}
else {
let descPart2 = symbol + addBrackets(to: pendingComboOperation!.descPart2)
}
pendingComboOperation = PendingComboOperation(descPart1: description!, descPart2: descPart2)
并出现错误:
使用未解析的标识符' descPart2'
为什么呢?我该如何解决?
答案 0 :(得分:0)
您无法在if
或else
语句中声明变量。解决方案是给descPart2
一个默认值:
var descPart2: String
if(pendingComboOperation == nil){
if(accumulator != nil){
descPart2 = symbol + String(accumulator!)
} else {
descPart2 = ""
}
} else {
descPart2 = symbol + addBrackets(to: pendingComboOperation!.descPart2)
}
pendingComboOperation = PendingComboOperation(descPart1: description!, descPart2: descPart2)