9
私はちょうどスウィフトの最新バージョンに私のアプリのコードを更新し、私はこの機能を持っている:単項演算子 ' - 'は、 '@ lvalue Int?'型のオペランドには適用できません。 (別名「@lvalueオプション<Int>」)
私は次の行でエラーメッセージを取得していますがfunc setupGraphDisplay() {
//Use 7 days for graph - can use any number,
//but labels and sample data are set up for 7 days
//let noOfDays:Int = 7
//1 - replace last day with today's actual data
graphView.graphPoints[graphView.graphPoints.count-1] = counterView.counter
//2 - indicate that the graph needs to be redrawn
graphView.setNeedsDisplay()
maxLabel.text = "\((graphView.graphPoints).max()!)"
print((graphView.graphPoints).max()!)
//3 - calculate average from graphPoints
let average = graphView.graphPoints.reduce(0, +)
/graphView.graphPoints.count
averageWaterDrunk.text = "\(average)"
//set up labels
//day of week labels are set up in storyboard with tags
//today is last day of the array need to go backwards
//4 - get today's day number
//let dateFormatter = NSDateFormatter()
let calendar = Calendar.current
let componentOptions:NSCalendar.Unit = .weekday
let components = (calendar as NSCalendar).components(componentOptions,
from: Date())
var weekday = components.weekday
let days = ["S", "S", "M", "T", "W", "T", "F"]
//5 - set up the day name labels with correct day
for i in (1...days.count).reversed() {
if let labelView = graphView.viewWithTag(i) as? UILabel {
if weekday == 7 {
weekday = 0
}
labelView.text = days[(weekday--)!]
if weekday! < 0 {
weekday = days.count - 1
}
}
}
}
:
私は答えをオンラインで検索してみましたが、私はまだ可能性Unary operator '--' cannot be applied to an operand of type '@lvalue Int?' (aka '@lvalue Optional<Int>')
:Xcodeのは私に次のエラーを与えている
labelView.text = days[(weekday--)!]
このエラーを修正するのに役立つものは何も見つかりません。
また、エラーメッセージが正確に意味するものは、@lvalue Int (aka '@lvalue Optional<Int>')
とは何ですか?私はこれまでこのデータ型を見たことがないし、それに応じて問題を解決する方法も知らない。
ありがとうございます。
スウィフトから '--'と' ++ 'が削除されました。すぐ上の行で 'weekday - = 1'を使います。 – par
Cスタイルのインクリメント/デクリメントやCスタイルのループの使用をやめなければならない – Pierce