cal中的额外参数错误[重复]

Posted

技术标签:

【中文标题】cal中的额外参数错误[重复]【英文标题】:extra argument error in cal [duplicate] 【发布时间】:2016-06-30 09:03:34 【问题描述】:

我目前正在使用 Swift 2.0 和 Xcode Beta 2 开发我的第一个 ios 应用程序。它读取外部 JSON 并在表格视图中生成包含数据的列表。但是,我遇到了一个似乎无法修复的奇怪小错误: 使用 swift 发布数据服务器的此代码中出现错误 (cal中的额外参数错误)

@IBAction func registerbutton(sender: AnyObject) 
    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool 
    //declare parameter as a dictionary which contains string as key and value combination.
    var parameters = ["name": Name.text!, "emailaddress": EmailAddress.text!, "phonenumber": PhoneNumber.text!, "Dealerloaction": dealerlocationtextfield.text!] as Dictionary<String, String>

    //create the url with NSURL
    let url = NSURL(string: "http://192.168.1.75:3002/users/sign_in") //change the url

    //create the session object
    var session = NSURLSession.sharedSession()

    //now create the NSMutableRequest object using the url object
    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST" //set http method as POST

    var err: NSError?
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err ) // pass dictionary to nsdata object and set it as request body

    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    //create dataTask using the session object to send data to the server
    var task = session.dataTaskWithRequest(request, completionHandler: data, response, error -> Void in
        print("Response: \(response)")
        var strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("Body: \(strData)")
        var err: NSError?
        var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary

        // Did the JSONObjectWithData constructor return an error? If so, log the error to the console
        if(err != nil) 
            print(err!.localizedDescription)
            let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("Error could not parse JSON: '\(jsonStr)'")
        
        else 
            // The JSONObjectWithData constructor didn't return an error. But, we should still
            // check and make sure that json has a value using optional binding.
            if let parseJSON = json 
                // Okay, the parsedJSON is here, let's get the value for 'success' out of it
                var success = parseJSON["success"] as? Int
                println("Succes: \(success)")
            
            else 
                // Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
                let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print("Error could not parse JSON: \(jsonStr)")
            
        
    )

    task.resume()
        return true

【问题讨论】:

能否请您附上实际的错误信息? 【参考方案1】:

Swift 中的错误处理不同于 Objective-C 中的错误处理。在 Swift 中, NSJSONSerialization.dataWithJSONObject 没有错误参数。它实际上会抛出异常并符合新的错误处理系统。

【讨论】:

以上是关于cal中的额外参数错误[重复]的主要内容,如果未能解决你的问题,请参考以下文章

“错误”教程书IOS 8 SDK开发第二版中的NSJSONSerialization额外参数[重复]

swift2:调用中的额外参数“错误”

MKPolygon 初始化错误“调用中的参数“interiorPolygons”缺少参数”/“调用中的额外参数”

Alamofire 请求收到错误“调用中的额外参数”

SwiftUI:随机“调用中的额外参数”错误

Alamofire发布请求错误调用中的额外参数“方法”