Expression at top level error

Hey!

Just started implementing iOS Cloud SDK to Xcode and I’m getting “Expressions are not allowed at top level error” with the most basic code from the docs:

ParticleCloud.sharedInstance().login(withUser: "username@email.com", password: "userpass") { (error:Error?) -> Void in
    if let _ = error {
        print("Wrong credentials or no internet connectivity, please try again")
    }
    else {
        print("Logged in")
    }
}

I also tried the same by making it a function, but got the same error.
What am I missing?

Thanks for the help.

I’m the Android dev here at Particle, but since our iOS dev is on vacation, I’ll do my best!

Given the error you’re getting, it seems like you need to wrap the code you’re writing into another structure like a function or a class. You mentioned trying to make it a function. Have you tried writing any other functions at the top level of a file to see if they work?

My best suggestion is to start from something that works, then try to add the Particle-specific code. One way to do that would be to find the simplest possible already-working iOS sample app you can (maybe by working through this tutorial from Apple?). Once you have that working, then try tossing that Particle snippet into one of the functions that’s already being called by the system, like a button callback, or on the viewDidLoad() method on a UIViewController.

Hope that helps!

1 Like