ios Swift - 表达式列表中的预期表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28574838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - Expected expression in list of expressions
提问by Number1
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.
我是 Swift 的新手,一直在阅读,但不知道这意味着什么。在下面的代码行中,我在参数 [String] 后面有“表达式列表中的预期表达式”。同样,它正在寻找“预期的','分隔符。我相信这些是相关的。
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
这是来自 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
}
采纳答案by gabbler
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"]) {
submitLacunaRequest(module: "empire", "login", ["myuserid", "mypassword", "mykey"]) {
回答by Yaroslav
I called my function parameter protocol
.
If I was to try using this property as usual, I would notice it to be written in pink being a keyword and I would rename it.
我调用了我的函数参数protocol
。如果我像往常一样尝试使用这个属性,我会注意到它用粉红色写成一个关键字,我会重命名它。
Instead, I used this property in a string like this and I didn't get any clues from the compiler:
相反,我在这样的字符串中使用了这个属性,但我没有从编译器那里得到任何线索:
func configure(_ protocol: Protocol, host:String, port:String) {
urlString = "\(protocol)://\(host):\(port)"
}
I spend good 5 minutes confused out of my mind by this error, but then I figured it out. The problem was in the name of the parameter.
我花了 5 分钟的时间被这个错误弄糊涂了,但后来我想通了。问题出在参数的名称上。
I didn't want to rename the parameter, so I ended up writing it like this:
我不想重命名参数,所以我最终这样写:
urlString = "\(`protocol`)://\(host):\(port)"
回答by Ronald Martin
You're calling the function incorrectly. You don't need the [String]
in the parameters
param...
您错误地调用了该函数。你不需要[String]
在parameters
PARAM ...
AppDelegate.submitLacunaRequest(module: "empire", method: "login", parameters: ["myuserid", "mypassword", "mykey"]) {
...
}