Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

for in loop error in new xcode

hi, import UIKit

var numbers:[Int] = [1,2,3,4,5,6,7,8,9,10]

for number in numbers { print("(numbers) x 7 = (numbers*7)") }

This is not working the error says "Binary operator '*' cannot be applied to operands of type 'int' and int

please help how do I fix it?

2 Answers

You are using the variable "numbers" in the print statement instead of the "number" for-in property. So the problem was that you were using an un-initialized variable, so just make sure to use the variable associated with the for-in loop.

import UIKit

var numbers:[Int] = [1,2,3,4,5,6,7,8,9,10]

for number in numbers { print("\(number) x 7 = \(number*7)") }

thanks alot for the fast response

I think you forgot to add your backslashes... You need them so the program knows that it needs to print the variable, not as an Int but a String.

import UIKit

var numbers:[Int] = [1,2,3,4,5,6,7,8,9,10]

for number in numbers { print("\(numbers) x 7 = \(numbers*7)") }

even with the back slash it's not working.