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

Jonathan Halbrook
Jonathan Halbrook
3,853 Points

Why is the challenge compiler rejecting my code when it compiles on playground?

On challenge task 3 of 4 for inserting and deleting values in Swift Collections and Flows the result of my code below was "Bummer! Your code could not be compiled." Preview gave this error message ( swift_lint.swift:11:12: error: expression resolves to an unused l-value arrayOfInts[4] ).

It compiles properly on Xcode so is there some type of bug in the challenge compiler or am I just not seeing my mistake?

Task 3 of 4 - We also learned about reading values from an array. Retrieve the 5th item (remember array indexes start at 0) and assign the result to a constant named value.)

My code - var arrayOfInts = [2, 4, 6, 8, 10, 12]

arrayOfInts.append(14)

arrayOfInts += [16]

arrayOfInts[4]

let value = arrayOfInts[4]

Link to page https://teamtreehouse.com/library/swift-3-collections-and-control-flow/introduction-to-collections/working-with-arrays

Thanks for your help!

2 Answers

Moritz Lang
Moritz Lang
25,909 Points

Hi, the following code did work for me:

var arrayOfInts = [0, 34, 57, 54, 89, 456]
arrayOfInts.append(56)
arrayOfInts += [19]

let value = arrayOfInts[4]

// task 4
let discardedValue = arrayOfInts.remove(at: 5)

Your mistake has to be writing arrayOfInts[4] in one line as this isn't valid Swift code. You can use this however in Playgrounds to get immediate results. In a real Xcode project this would be wrong code and you'd get a compiler error.

Jonathan Halbrook
Jonathan Halbrook
3,853 Points

Thanks so much! After I took out the line "arrayOfInts[4]" everything worked.