問題描述
Xcode Playground 只能部分運行 (Xcode Playground can only run partially)
如上所示,在 Xcode 操場上,我正在嘗試擴展協議 Exercise 以獲得另外兩個計算屬性,caloriesBurnedPerMinute 和 描述。當我想在操場上運行這部分代碼時,最左下角的“播放按鈕”顯示為灰色,表示直到第20行才能運行代碼。但是,如果我只想運行代碼直到第14行,如下圖,“播放按鈕”顯示為藍色,表示可以運行代碼到第14行。
here.
It's a way of running your code line by line, but there are some limitations. If you hover over the line number "20", you will see a grey line:
That means Xcode can't only run that part of the code, presumably because how Xcode handles extension declarations.
Just add some code that actually runs, as opposed to just declarations, and press the play button above the bottom panel. Your code will run just fine.
方法 2:
You need to wrap the Double
variables in a String
cast like String(caloriesBurned)
:
extension Exercise {
var description: String {
return "Exercise(\(name), burned \(String(caloriesBurned)) calories in \(String(minutes)) minutes)"
}
}
I don't know why, but it helps