が動作していないまで
@IBAction private func buttonpress(sender: UIButton)
{
let digit = sender.currentTitle!
if userisinthemiddle
{
let currenttext = display.text!
display.text = currenttext + digit
}
else
{
display.text = digit
}
userisinthemiddle = true
}
を動作しませんでしたあなたのプログラムが失敗した場合に実際にプログラムをクラッシュさせない限り、変数のアンラップを強制することをお勧めします(私の経験ではまれです)。この場合、問題を診断する能力が阻害されているようです。 (1)力のアンラッピングを避け、(2)予期しない値に反応するより良い位置にいるようにしてください。
@IBAction private func buttonPress(sender: UIButton)
{
guard let digit = sender.currentTitle else
{
assertionFailure("digit is nil.")
return
}
print("digit: \(digit)"
if userIsInTheMiddle
{
let currentText = display.text ?? "" // If display.text is nil, set currentText to an empty string
print("currentText: \(currentText)"
display.text = currentText + digit
}
else
{
display.text = digit
}
print("display.text: \(display.text)"
userIsInTheMiddle = true
}
var displayValue: Double
{
get
{
let text = display.text ?? ""
guard let double = Double(text) else
{
// You probably want to replace this `assertionFailure` and return a default value like 0
assertionFailure("text could not be converted to a Double")
return
}
return double
}
set
{
display.text = String(newValue)
}
}
いくつかの質問:
@IBAction
に関連displayValue
財産をどのように?
+
演算子を使用して、ここで2つの文字列を連結していますdisplay.text = currentText + digit
。 2つの数字を追加しようとしていないことを確認するだけですか?
'display.text'が' 'abcdef" 'の場合はどうなりますか? – luk2302
'text'は' nil'であるか、文字列は 'Double'で表現できません。それを確認します。 – vadian
クラッシュを避けるために、* nilコアレス演算子* '??'を強制アンラップ '!'の代わりに使うとよいでしょう。 'Double(display.text ??" ")?? 0 'となる。 – vacawama