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 trialPatrizio Chiquini
823 PointsI keep getting some errors. I have rewatched and remade my project 3 times but same messages come up
I get an error in each line of code (provide below):
if let plistPath = NSBundle.mainBundle().pathForResource("CurrentWeather", ofType: "plist"),
//ERROR: expected '{' after 'if'
let weatherDictionary = NSDictionary(contentsOfFile: plistPath),
//ERROR: use unresolved identifier 'plistPath'
let currentWeatherDictionary = weatherDictionary["currently"] as? [String: AnyObject]{
//ERROR: 'let' cannot appear nested inside another 'var' or 'let' pattern
let currentWeather = CurrentWeather(weatherDictionary: currentWeatherDictionary)}
//ERROR: use of unresolved identifier 'currentWeatherDictionary'
am I overseeing something?! please help thank you
3 Answers
Greg Kaleka
39,021 PointsHi Patrizio,
Disclaimer
I have not tested this, and Swift 1.2 handles this differently than previous versions... so just try this and see if it works, then report back :).
This error is the best clue:
//ERROR: 'let' cannot appear nested inside another 'var' or 'let' pattern
You need to remove the extra lets from the statement. Should look like this:
if let thingOne=1, thingTwo=2, thingThree=3 {
// do stuff
}
Patrizio Chiquini
823 PointsThat's what I was afraid of.... So after changing the syntax to:
1 if let plistPath = NSBundle.mainBundle().pathForResource("CurrentWeather", ofType: "plist"),
2
3 weatherDictionary = NSDictionary(contentsOfFile: plistPath),
4
5 currentWeatherDictionary = weatherDictionary["currently"] as? [String: AnyObject]
6 {
7 let currentWeather = CurrentWeather(weatherDictionary: currentWeatherDictionary)
8 }
There only exist to errors. First error is on line 1 [ERROR: Expected '{' after 'if' condition] Second error is on line 6 [ERROR: braced block of statement is an unused closure]
I guess the question has now become "how can I make this nested if statement work properly?"
Greg Kaleka
39,021 PointsThe answer will be in the documentation. Step 1: figure out which version of XCode and Swift you are running.
Patrizio Chiquini
823 PointsXCode Version: Version 6.1.1 Swfit Version: 1.1
alexvalladares
21,478 PointsWell, just make a classic nested if and issue is solved!
if let plistPath = NSBundle.mainBundle().pathForResource("CurrentWeather", ofType: "plist") {
if let weatherDictionary = NSDictionary(contentsOfFile: plistPath) {
if let currentWeatherDictionary = weatherDictionary["currently"] as? [String: AnyObject] {
let currentWeather = CurrentWeather(weatherDictionary: currentWeatherDictionary)
currentTemperatureLabel?.text = "\(currentWeather.temperature)ΒΊ"
currentHumidityLabel?.text = "\(currentWeather.humidity)%"
currentPrecipitationLabel?.text = "\(currentWeather.precipProbability)%"
}
}
}
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsNote: I just watched the video linked to this question. Looks like you and Pasan are using different versions of Xcode/Swift. Swift is a very young language, and is changing a lot! There may have been a video earlier on where Pasan mentions which versions of the software he's using. I would recommend matching those so you can follow along more smoothly.