ios - Swift - 表达式列表中的预期表达式

标签 ios regex swift

我是 Swift 的新手,一直在阅读,但不知道这意味着什么。在下面的代码行中,我在参数 [String] 之后有“Expected expression in list of expressions”。同样在同一点,它正在寻找“预期的‘,’分隔符。我相信这些是相关的。

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)")
                }
            }
        }

这里是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
}

最佳答案

您在调用函数时使用了外部参数名称作为参数,但外部参数未在您的函数声明中定义。只需以这种方式使用它。

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

关于ios - Swift - 表达式列表中的预期表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28574838/

相关文章:

ios - 在 Swift 中增加 UIView.animateWithDuration 中的参数

ios - 如何使用Swift 3.0读取GB2312编码的文本文件

c++ - 正则表达式仅匹配匹配组中许多可能性中的前两种

c# - 如何在 C# 中找到两个标记字符之间的可变长度字符串?

ios - 如何正确加载来自 Firebase 的信息?

ios - 在 swift UIWebKit 中处理外部链接?

ios - 如何制作包含 UILabel 的可重用 UIView 模板

ios - 如何将照片从一个 View Controller 传输到另一个 View Controller 的 UIImageView

python - 优化Python字节数组转义性能

Swift 函数前向声明或原型(prototype)