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

Statement are not allowed at the top level

In the Code challenge with the movie and the lead actor, im getting the following error:

"Statement are not allowed at the top level"

My code for the challenge is :

let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "Léa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]

var leadActor: String = ""

if let theMovie = movieDictionary["Spectre"],
    let movieCast = theMovie["cast"] {
        leadActor = movieCast[0]
}

Whats wrong here?

3 Answers

Okay I looked around and found this answer:

"Because it is a snippet. You are expected to use it inside a function, where it is legal. Executable code can appear only in a function."

http://stackoverflow.com/questions/30880024/swift-error-statements-are-not-allowed-at-the-top-level

So maybe if you put it inside a function in xcode, it will work? Like in the viewDidLoad method when making an app?

good one! it works if you put the code in a function (any function)

Thanks for your help :)))

Hi Marina & Ian,

Thank you for your answers.

In Xcode I used the following code but it didn't pass the challenge. Do you think it's correct?

let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "Léa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]

var leadActor: String = ""

if let someActor = movieDictionary["Spectre"],
    let leadActor = someActor["cast"]?[0] {
        print(leadActor)
} else {
        print("This is not the lead actor")
}

Thanks! Mitchell

Hi Marina & Ian,

Thank you for your answers.

In Xcode I used the following code but it didn't pass the challenge. Do you think it's correct?

let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "Léa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]

var leadActor: String = ""

if let someActor = movieDictionary["Spectre"],
    let leadActor = someActor["cast"]?[0] {
        print(leadActor)
} else {
        print("This is not the lead actor")
}

Thanks! Mitchell

Hi Mitchell

Are you trying to solve the challenge or just playing around with the code in xcode? The optional is not necessary between the cast key and the index number. And also, you need to access the cast before you can retrieve the lead actor, like in the code Alexandre posted. Then you can move on to retrieving leadactor and printing your statement.

''' let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "Léa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]

var leadActor: String = ""

// Enter your code below...

if let movie = movieDictionary["Spectre"] { if let cast = movie["cast"] { leadActor = cast[0] }

}

'''

Just got this to go through I can't believe I've been an idiot for like 3 days not getting it

Hi

Here is the answer that worked for me. I can't quite see the difference from yours, so maybe it's something small..

let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "Léa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]

var leadActor: String = ""

if let movie = movieDictionary["Spectre"],
    let moviecast = movie["cast"] {
        leadActor = moviecast[0]
}

Hi Marina, thx for your answer but this still show the error.

I forgot to mentions that I have this error on xcode 7.2 playground. I passed the challenge with the code but just want to figure out why Xcode does not like it :)

Ah, yes. I see what you mean - I get the same error. Just had it in another challenge where the if let statement is used. Brendan Whiting, maybe you know this? :-)

I don't know the answer, I'm sorry. And I don't know what that error message means. What challenge is it? From my experience, if it works in Xcode but not in the challenge it is one of two things

1) Xcode is the new version of Swift, the challenge is the older Swift course with the older version of Swift

2) The code works, but it doesn't produce the result that the challenge is asking for.

Ah okay, well worth a shot ;-) It's the challenge "nil values in collections". And the problem is not the challenge but xcode - the error is in xcode, the challenge doesn't give an error. I've seen this error before in xcode when using if let... Maybe someone else will have the answer :-)