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

Amitai Blickstein
Amitai Blickstein
6,863 Points

My code doesn't pass, with the error: "bummer!". Like, f'shur'totally, duuuude.

let plistPath: String = NSBundle.mainBundle().pathForResource("CrazyInformation", ofType: "plist")! let myDict = NSDictionary(contentsOfFile: plistPath)

implicit unwrapping [if lets (and guards)] didn't work either.

ty in advance!

plist.swift
import Foundation

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

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Amitai Blickstein,

You're close! First, remove your explicit type declaration of String for plistPath object. The type of object is NSBundle. Second, because initialization of a dictionary can fail, we need to safely initialize it using optional binding. Use an if let to make sure that NSBundle.mainBundle().pathForResource() doesn't return nil, if it does not - then you can safely initialize the dictionary because you know a value for plistPath exists.

import Foundation

// Add your code below
if let plistPath = NSBundle.mainBundle().pathForResource("CrazyInformation", ofType: "plist") {
  let crazyDict = NSDictionary(contentsOfFile: plistPath)
}

Good Luck! Hope this helps!

Amitai Blickstein
Amitai Blickstein
6,863 Points

Ah, thanks. if let probably didn't work for me before because of my explicit type declaration...