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 trialtmsmth
10,543 PointsWhy we need to write "atIndex" instead of todo.insert("element", 3) for example ?
I already used other programming language and the function "knows" that the second argument would be the index so why we need to write "atIndex:", whereas we dont write "newElement:" before the first argument. Thanks !
2 Answers
kjvswift93
13,515 PointsatIndex is the required parameter name for apple's provided function "insert". If you were to attempt to insert a String into an array of Strings at the second position, or 3rd index, using
todo.insert("element", 3)
Then the Int 3 is unresolved and doesn't signify anything. It needs to signify the placement in the array that "element" should be inserted- "3" by itself doesn't do that. Only by using the atIndex keyword does the number 3 represent a 3rd index or second placement in an array.
tmsmth
10,543 PointsYes I got that I need to write it, I was just suggesting that the function could know that the second argument is the index, without writing "atIndex" !
Thanks for the answer btw !
Nick Radford
2,610 PointsYeah, I agree... seems like a remainder from Objective-C's syntax.