Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Expected expression in list of expressions

Tags:

regex

ios

swift

I'm new to Swift been reading but have no clue what this means. On the line of code below, I have "Expected expression in list of expressions" after parameters[String]. AS well at the same point it is looking for "Expected ',' separator. I believe these are related.

AppDelegate.submitLacunaRequest(module: "empire", method: "login", parameters[String]:["myuserid", "mypassword", "mykey"]) {

            responseObject, error in

            // some network error or programming error

            if error != nil {
                println("error = \(error)")
                println("responseObject = \(responseObject)")
                return
            }

            // network request ok, now see if login was successful

            if let responseDictionary = responseObject as? NSDictionary {
                if let errorDictionary = responseDictionary["error"] as? NSDictionary {
                    println("error logging in (bad userid/password?): \(errorDictionary)")
                } else if let resultDictionary = responseDictionary["result"] as? NSDictionary {
                    println("successfully logged in, refer to resultDictionary for details: \(resultDictionary)")
                } else {
                    println("we should never get here")
                    println("responseObject = \(responseObject)")
                }
            }
        }

Here is the related code from AppDelegate

public func submitLacunaRequest (#module: String, method: String, parameters: AnyObject, completion: (responseObject: AnyObject!, error: NSError!) -> (Void)) -> NSURLSessionTask? {

    let session = NSURLSession.sharedSession()
    let url = NSURL(string: "https://us1.lacunaexpanse.com")?.URLByAppendingPathComponent(module)
    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"
    request.setValue("application/json-rpc", forHTTPHeaderField: "Content-Type")


    let requestDictionary = [
        "jsonrpc" : "2.0",
        "id"      : 1,
        "method"  : "login",
        "params"  : ["myuserid", "mypassword", "mykey"]
    ]

    var error: NSError?
    let requestBody = NSJSONSerialization.dataWithJSONObject(requestDictionary, options: nil, error: &error)
    if requestBody == nil {
        completion(responseObject: nil, error: error)
        return nil
    }

    request.HTTPBody = requestBody

    let task = session.dataTaskWithRequest(request) {
        data, response, error in

        // handle fundamental network errors (e.g. no connectivity)

        if error != nil {
            completion(responseObject: data, error: error)
            return
        }

        // parse the JSON response

        var parseError: NSError?
        let responseObject = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parseError) as? NSDictionary

        if responseObject == nil {

            // because it's not JSON, let's convert it to a string when we report completion (likely HTML or text)

            let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) as String
            completion(responseObject: responseString, error: parseError)
            return
        }

        completion(responseObject: responseObject, error: nil)
    }
    task.resume()

    return task
}
like image 919
Number1 Avatar asked Feb 18 '15 02:02

Number1


2 Answers

You are using external parameter name for a parameter when calling the function, but the external parameter is not defined in your function declaration. Simply use it this way.

submitLacunaRequest(module: "empire", "login", ["myuserid", "mypassword", "mykey"]) {

like image 147
gabbler Avatar answered Oct 05 '22 14:10

gabbler


You're calling the function incorrectly. You don't need the [String] in the parameters param...

AppDelegate.submitLacunaRequest(module: "empire", method: "login", parameters: ["myuserid", "mypassword", "mykey"]) {
    ...
}
like image 30
Ronald Martin Avatar answered Oct 05 '22 13:10

Ronald Martin