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 trialAlison King
iOS Development with Swift Techdegree Student 424 Pointsarray challenge 3
my code;
var arrayOfInts = [1, 2, 3, 4, 5, 6]
arrayOfInts.append(7)
arrayOfInts + [8] arrayOfInts = arrayOfInts + [8] arrayOfInts arrayOfInts[5] let value = arrayOfInts[5]
this works in my playground to give correct result but when i submit it it says its wrong. Why?????
// Enter your code below
var arrayOfInts = [1, 2, 3, 4, 5, 6]
arrayOfInts.append(7)
arrayOfInts + [8]
arrayOfInts = arrayOfInts + [8]
arrayOfInts
arrayOfInts[5]
let value = arrayOfInts[5]
2 Answers
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Alison,
Technically you code is not violating the Swift language specifications, it's just not what you'd want in real code. There is a plugin called SwiftLint which forces the compiler to reject some code that violates style guidelines or best practice but isn't strictly invalid Swift.
It is this linter that Treehouse is running and rejects your code, not the Swift language itself. If you added SwiftLint to your Xcode installation, your Playground would then throw up the same errors on your local machine as are being returned by the Treehouse backend.
Note that I'm not necessarily recommending that you install SwiftLint, just that this is why you are getting a different experience on your local system than you are getting from Treehouse.
As for testing whether code is correct, yes, Playgrounds are for doing that but there are some caveats to bear in mind. The Playground will tell you if your code is incorrect in certain ways, but it not's possible to identify every way that code could possibly be invalid or behave differently than expected.
The instances in your code where you have unused expressions are cases where the code is syntactically valid (so won't cause the Swift language to reject it) but not structured in a way that you'd ever use in a real program. However, because we all frequently do in Playgrounds what you've done: write a simple expression just to see what will appear in the results pane, I can see why Playgrounds doesn't, by default, throw up an error or warning.
Hope that's a bit clearer
Alex
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Alison,
Your problem in steps 1 and 2 of the challenge is that you are using a code construct that works in Playgrounds but doesn't make sense outside of Playgrounds, so the Swift Linter is complaining.
You have three lines that share this same problem. Let's look at the first instance of it:
arrayOfInts + [8]
It makes sense to write this in Playgrounds because when the expression is evaluated, the result is displayed on the right side of the screen. However, when we're not in Playgrounds this sort of expression does nothing: it doesn't change any state or display anything to the screen. Swift assumes that in real code you'd never want to have a line of code that doesn't really do anything, so that's why it's giving you that error.
Compare this to the very next line, which is perfectly valid because you are doing something with the evaluated expression, in this case assigning it back to that variable:
arrayOfInts = arrayOfInts + [8]
So, to make the first two steps of the challenge pass, you'll need to remove the lines of code that don't do anything.
You'll find that your code still doesn't pass step 3, and I would suggest you re-read these words from the instructions for a clue as to why:
remember array indexes start at 0
Hope that clears everything up for you
Cheers
Alex
Alison King
iOS Development with Swift Techdegree Student 424 PointsHi Alex
Ok thanks - Now I have done it but I was under the impression that this is what the Playground is for - to test that the code is correct. Thats why I was so confused. So how do we know when the compiler thinks the code is relevant or not? what then is the point of using the playground at this stage as it seems to make things more confusing?
Alison King
iOS Development with Swift Techdegree Student 424 PointsAlison King
iOS Development with Swift Techdegree Student 424 PointsOK Alex Thanks very much for the very clear explanation. Thats actually big help, especially when we are at the beginning of this journey.