gpt4 book ai didi

swift - 一元运算符 '--' 不能应用于 '@lvalue Int?' 类型的操作数(又名 '@lvalue Optional' )

转载 作者:IT王子 更新时间:2023-10-29 05:12:59 26 4
gpt4 key购买 nike

<分区>

我刚刚将我的应用程序代码更新到最新版本的 Swift,并且我有这个功能:

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
}
}
}
}

但是我在以下行收到一条错误消息:

labelView.text = days[(weekday--)!]

Xcode 给我以下错误:

Unary operator '--' cannot be applied to an operand of type '@lvalue Int?' (aka '@lvalue Optional<Int>')

我尝试在线搜索答案,但仍然找不到任何可以帮助我修复此错误的信息。

我也很好奇 @lvalue Int (aka '@lvalue Optional<Int>') 类型的错误消息到底意味着什么?我以前从未见过这种数据类型,也不知道如何相应地解决问题。

提前致谢。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com