Reputation: 21
I'm trying to write code that will update my array and give total pay based on the daily pay. I'm getting an error about binary operators so how do I fix this line code so that doesn't happen.
for day in stride(from: 1, to: 31, by: 1)
{
dailyPay[day] = [Int(pay)]
pay*=2
if(day==1)
{
totalPay[day] = Int(pay)
}
else
{
totalPay[day] = totalPay[day-1]+dailyPay[day]//The problem is Here
print("\(heade) \(day) \(head) \(dailyPay[day]) \(total) \(totalPay[day])")
}
Upvotes: 1
Views: 460
Reputation: 17040
You don't show the declarations of your variables, but it appears that totalPay
is an array of Int
s, whereas dailyPay
is a two-dimensional array of arrays of Int
. So, totalPay[day-1]
will be an Int
, whereas dailyPay[day]
will be an [Int]
, or an array of Int
s. The error you're getting therefore means exactly what it says; you can't use +
to add an Int
and an array.
From your code, it appears that dailyPay
is probably meant to be a plain old array of integers, like totalPay
. So you could fix this by changing the declaration, whereever it is, from:
var dailyPay: [[Int]]
to:
var dailyPay: [Int]
Then, change the assignment to:
dailyPay[day] = Int(pay)
and things should work.
Sidenote: Your for
loop is needlessly complex. There's no need for stride
, when you can just:
for day in 1...31
Upvotes: 3