swift,使用未解析的标识符

时间:2018-01-01 19:05:49

标签: swift swift3

我有一个快速的代码

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'

为什么呢?我该如何解决?

1 个答案:

答案 0 :(得分:0)

您无法在ifelse语句中声明变量。解决方案是给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)