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 Build a Weather App with Swift Simple Data Structures Loading Files From A Directory

David Elliott
David Elliott
2,343 Points

exersize not working

import Foundation

// Add your code below let plistPath = NSBundle.mainBundle().pathForResource("CrazyInformation", ofType: "plist") let dictionary = NSDictionary(contentsOfFile: plistPath!)

//this code throws an error in the website but works fine in my xcode project

plist.swift
import Foundation

// Add your code below
let plistPath = NSBundle.mainBundle().pathForResource("CrazyInformation", ofType: "plist")
let dictionary = NSDictionary(contentsOfFile: plistPath!)

4 Answers

Hi David,

I confirmed it still works. Check out this answer by Martin Wildfeuer.

David Elliott
David Elliott
2,343 Points

Hi Aaron, Thanks for checking. The answer you link to suggests I answer in the same way that I did. Thats why I thought it was a bug in the website. oh well

Did you try adding it to a If statement?

if let plistPath = NSBundle.mainBundle().pathForResource("CrazyInformation", ofType: "plist") {
   let dictionary = NSDictionary(contentsOfFile: plistPath)
   }
David Elliott
David Elliott
2,343 Points

I didn't, force unwrap should be the same thing right?

It should. But it's not. So try the If statement to get past it :) I'll send a note to the support team, asking them to take a look at this challenge.

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

Hey David Elliott,

Force unwrapping isn't really the same thing. It's only the same when you have the right path. If your resource doesn't exist and you force unwrap that will cause a crash.

In this particular case the error message should be notifying you of that (since there isn't actually a CrazyInformation.plist in the directory). I'll take a look at that and get it sorted

Thank you for clearing that up for us Pasan Premaratne

David Elliott
David Elliott
2,343 Points

Pasan Premaratne thanks that makes sense. I assumed that the force unwrap would be equivalent to conditional assignment since I also assumed that there was a CrazyInformation.plist in the directory.