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

George W
George W
2,697 Points

Not sure what to do with plistPath

I'm not really sure what i'm supposed to be doing in this because this code isn't working. Did I miss something? I even tried using the code from the weather project and modifying it but it hasnt worked. Please help. Thanks

plist.swift
import Foundation

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

1 Answer

Michael Reining
Michael Reining
10,101 Points

Hi George,

You are close. The challenge does not say so specifically but you are dealing with optionals here and the trick is to safely unwrap the optional. To unwrap an optional use if let like this.

// if let basically means
// if plistPath is not nil... then create the crazyDictionary

if let plistPath = NSBundle.mainBundle().pathForResource("CrazyInformation", ofType: "plist") {
    let crazyDictionary = NSDictionary(contentsOfFile: plistPath)
}

I hope that helps. I created an app helping others learn swift and it includes a chapter on optionals that might help.

Mike

PS: Thanks to the awesome resources on Team Treehouse, I launched my first app.

Code! Learn how to program with Swift

Hello Im new to swift as well, can you also unwrap with the ? symbol

Michael Reining
Michael Reining
10,101 Points

The question mark does not unwrap an optional. An optional is a separate type. It says that something is either there or not. Once unwrapped, you either have a nil value or the real thing.

Here are two ways to unwrap an optional.

let optionalInt: Int?
optionalInt = 5

// unwrap using if let
if let realInt = optionalInt {
    print(realInt) // safely unwraps since it only exists if optional is not nil
}

// unwrap if not nil
if optionalInt != nil {
    print(optionalInt!) // force unwrap since we know it is not nil from above
}

I hope that helps,

Mike