Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Error Handling in Swift 2.0 Error Handling Cleaning Up With Defer

Ryan Maneo
Ryan Maneo
4,342 Points

Come again?

While loops can have let after them to check for errors too? Omitted knowledge... Loops can be coupled with a try statement? Omitted knowledge. Where is the readFile function? Omitted knowledge. Someone please clarify and fill me in... I feel so confused all of a sudden!

Code I'm talking about: http://i.imgur.com/ld7PiUn.png

1 Answer

That definitely isn't omitted knowledge. In the code you're referring to, he's simply calling the file.readLine() method, which from this context seems to return a boolean value depending on whether there are more lines. The reason he is assigning it to a constant is because it allows him to access the result of the function inside the while loop. While he didn't explicitly mention any of this in the video, it's common sense that you can use what is returned from a method in a while loop as the boolean value.

The readLine method doesn't need to be explicitly mentioned - it's coupled with the file class for use with files. We aren't specifically working with files in anything we are doing, and he used this purely as an example, which is why he didn't feel the need to dive further into the information.

Paul Karim
Paul Karim
3,428 Points

Hey jake I think what ryan meant was that we never used that kind of logic in the course for while loops or in the rest of the course so far. So if you're new to programming and only see while loops as being able to do things like while x > y etc seeing it binded like that might seem a bit odd.

Don't worry ryan it will make more sense in the future i'm sure :)

Paul Karim Ah, understood. And yeah, don't worry too much about it Ryan. Through the rest of the courses, you'll gain exposure to many other abilities through the different functionalities of Swift and iOS development. If you see something you haven't before, just take it into account. If the question isn't answered later, ask it here :)