2

I have a very simple question that I cannot find the answer to. I am trying to plug an array of x and y values through an equation, and fill an array with the answers. The error in this code is that the arrays don't accept numbers because "error: 'Range' is not convertible to 'Int'". How would I deal with this? This is done with Xcode 6.01 in swift playground. Thanks!

let x = [5.0,2.0,43.0,1.0,5.0]
let y = [62.0,2.0,43.0,1.0,4.0]
var answers=[Double]()
var current:Double = 0

for numbers in [0...4] {
    current = 1800*x[numbers] + 1600*y[numbers]
    answers.append(current)
} 

println(answers)

1 Answer 1

2

A range is simply noted by a ... b or a ..< b (depending on whether the last element is included in the range or not). Therefore

for numbers in [0...4] {

should be

for numbers in 0...4 {

without the square brackets, or better

for numbers in 0 ..< x.count {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.