调用中缺少参数jsonData

时间:2016-06-22 19:36:53

标签: ios swift model-view-controller swift2

我在tableviewcontroller类中遇到错误,该错误表示

  

在调用中缺少参数jsonData的参数。

这就是json的样子

{  
   "success":true,
   "comments":{  
      "comments":[  
         {  
            "id":"486",
            "action":"70",
            "message":"Thanks for being so fun to work with!<br><img class=\"gifImg\" src=\"https:\/\/media1.giphy.com\/media\/JftflMX47tfck\/giphy.gif\">",
            "impact":"0",
            "sender":"2",
            "type":"comment",
            "date":"2016-06-17 10:09:11",
            "senderName":"Nick Helton",
            "senderId":"2",
            "senderTitle":"CTO",
            "senderImgTime":"1442506570",
            "actionName":"Make work fun",
            "behavior":"9",
            "likes":[  

            ],
            "replies":[  

            ],
            "totalLikes":"0",
            "recipients":[  
               {  
                  "id":"1053",
                  "name":"Vignesh Krish",
                  "title":"Software Developer Intern",
                  "imgTime":"1462805814"
               }
            ]
         },
         {  
            "id":"473",
            "action":"66",
            "message":"Notorious VIG&nbsp; - the work on that new calculator widget is amazing. Thank you so much!<img class=\"gifImg\" src=\"https:\/\/media3.giphy.com\/media\/3oEdva9BUHPIs2SkGk\/giphy.gif\">",
            "impact":"0",
            "sender":"1038",
            "type":"comment",
            "date":"2016-06-09 13:18:02",
            "senderName":"Craig Spilker",
            "senderId":"1038",
            "senderTitle":"VP Business Development and Sales",
            "senderImgTime":"1441129118",
            "actionName":"Don&#8217;t be a follower, think different",
            "behavior":"8",
            "likes":[  
               {  
                  "user":"5",
                  "name":"Clint Carlos",
                  "score":"1"
               },
               {  
                  "user":"1038",
                  "name":"Craig Spilker",
                  "score":"1"
               }
            ],
            "replies":[  
               {  
                  "message":"You're welcome Craig!!",
                  "date":"2016-06-09 14:44:00",
                  "senderName":"Vignesh Krish",
                  "senderId":"1053"
               }
            ],
            "totalLikes":"2",
            "recipients":[  
               {  
                  "id":"1053",
                  "name":"Vignesh Krish",
                  "title":"Software Developer Intern",
                  "imgTime":"1462805814"
               }
            ]
         },
         {  
            "id":"470",
            "action":"70",
            "message":"Vignesh - Happy Birthday yesterday. Glad we got to have some fun on the lake. Keep up the good work!<img class=\"gifImg\" src=\"https:\/\/media4.giphy.com\/media\/3o85xAYQLOhSrmINHO\/giphy.gif\">",
            "impact":"0",
            "sender":"5",
            "type":"comment",
            "date":"2016-06-09 09:07:50",
            "senderName":"Clint Carlos",
            "senderId":"5",
            "senderTitle":"CEO",
            "senderImgTime":"1450201931",
            "actionName":"Make work fun",
            "behavior":"9",
            "likes":[  

            ],
            "replies":[  
               {  
                  "message":"thank you Clint! It was awesome!!:-)<img class=\"gifImg\" src=\"https:\/\/media0.giphy.com\/media\/31dEqYHnutIek\/giphy.gif\">",
                  "date":"2016-06-09 10:34:50",
                  "senderName":"Vignesh Krish",
                  "senderId":"1053"
               }
            ],
            "totalLikes":"0",
            "recipients":[  
               {  
                  "id":"1053",
                  "name":"Vignesh Krish",
                  "title":"Software Developer Intern",
                  "imgTime":"1462805814"
               }
            ]
         },
         {  
            "id":"468",
            "action":"68",
            "message":"Great job and thanks for all your work on the app. So excited for what's to come!",
            "impact":"0",
            "sender":"3",
            "type":"comment",
            "date":"2016-06-01 17:16:14",
            "senderName":"Collin Caneva",
            "senderId":"3",
            "senderTitle":"Co-founder",
            "senderImgTime":"1393864410",
            "actionName":"Simplicity is the best measure of genius",
            "behavior":"8",
            "likes":[  
               {  
                  "user":"1038",
                  "name":"Craig Spilker",
                  "score":"3"
               }
            ],
            "replies":[  
               {  
                  "message":"Looks awesome!!",
                  "date":"2016-06-06 10:19:19",
                  "senderName":"Craig Spilker",
                  "senderId":"1038"
               }
            ],
            "totalLikes":"1",
            "recipients":[  
               {  
                  "id":"1053",
                  "name":"Vignesh Krish",
                  "title":"Software Developer Intern",
                  "imgTime":"1462805814"
               }
            ]
         },
         {  
            "id":"466",
            "action":"75",
            "message":"Team - Hope you all have a great holiday weekend. I'm proud of the progress and accomplishments you've made over the past weeks, including new features, new client launches, closing new deals, creating blog content, adding to our sales pipeline, marketing efforts, closing investors and making hard decisions about our future. You've pulled together as a group and we are pressing forward at a faster pace. Good work. Enjoy the time with your friends and families and let's be ready to achieve even more next week. Love having you all on the team.<img class=\"gifImg\" src=\"https:\/\/media3.giphy.com\/media\/1YfXT6vtAgQKI\/giphy.gif\">",
            "impact":"0",
            "sender":"5",
            "type":"comment",
            "date":"2016-05-28 09:04:30",
            "senderName":"Clint Carlos",
            "senderId":"5",
            "senderTitle":"CEO",
            "senderImgTime":"1450201931",
            "actionName":"Possess indomitable fortitude",
            "behavior":"11",
            "likes":[  
               {  
                  "user":"3",
                  "name":"Collin Caneva",
                  "score":"2"
               },
               {  
                  "user":"1038",
                  "name":"Craig Spilker",
                  "score":"2"
               },
               {  
                  "user":"1047",
                  "name":"Stephanie Whitsett",
                  "score":"1"
               }
            ],
            "replies":[  
               {  
                  "message":"<img class=\"gifImg\" src=\"https:\/\/media2.giphy.com\/media\/xT1XGPy39lDKJ5Gc5W\/giphy.gif\">",
                  "date":"2016-05-31 13:05:07",
                  "senderName":"Collin Caneva",
                  "senderId":"3"
               },
               {  
                  "message":null,
                  "date":"2016-06-01 14:21:54",
                  "senderName":"Shannon Wooten",
                  "senderId":"1039"
               }
            ],
            "totalLikes":"3",
            "recipients":[  
               {  
                  "id":"2",
                  "name":"Nick Helton",
                  "title":"CTO",
                  "imgTime":"1442506570"
               },
               {  
                  "id":"3",
                  "name":"Collin Caneva",
                  "title":"Co-founder",
                  "imgTime":"1393864410"
               },
               {  
                  "id":"6",
                  "name":"Aaron Davis",
                  "title":"Business Relations",
                  "imgTime":"1"
               },
               {  
                  "id":"1038",
                  "name":"Craig Spilker",
                  "title":"VP Business Development and Sales",
                  "imgTime":"1441129118"
               },
               {  
                  "id":"1039",
                  "name":"Shannon Wooten",
                  "title":"VP Operations",
                  "imgTime":"1"
               },
               {  
                  "id":"1045",
                  "name":"Colby  Coash",
                  "title":"Business Development",
                  "imgTime":"1446610053"
               },
               {  
                  "id":"1047",
                  "name":"Stephanie Whitsett",
                  "title":"Intern",
                  "imgTime":"1460056542"
               },
               {  
                  "id":"1052",
                  "name":"Alec Skinner",
                  "title":"Developer Intern",
                  "imgTime":"1465849319"
               },
               {  
                  "id":"1053",
                  "name":"Vignesh Krish",
                  "title":"Software Developer Intern",
                  "imgTime":"1462805814"
               }
            ]
         },
         {  
            "id":"460",
            "action":"68",
            "message":"Great job on the new modifications to the website! Glad your on the team and keep it going!<div><br><\/div><img class=\"gifImg\" src=\"https:\/\/media0.giphy.com\/media\/2Vp0UxNcA8TEQ\/giphy.gif\">",
            "impact":"0",
            "sender":"1043",
            "type":"comment",
            "date":"2016-05-23 10:28:45",
            "senderName":"Jane  Smith",
            "senderId":"1043",
            "senderTitle":"Intern",
            "senderImgTime":"1",
            "actionName":"Simplicity is the best measure of genius",
            "behavior":"8",
            "likes":[  
               {  
                  "user":"3",
                  "name":"Collin Caneva",
                  "score":"1"
               }
            ],
            "replies":[  

            ],
            "totalLikes":"1",
            "recipients":[  
               {  
                  "id":"1053",
                  "name":"Vignesh Krish",
                  "title":"Software Developer Intern",
                  "imgTime":"1462805814"
               }
            ]
         },
         {  
            "id":"452",
            "action":"72",
            "message":"Vignesh - welcome to the team and great job taking a risk and doing something new and challenging in building our mobile app. Can't wait to see the prototype and excited about your potential to innovate for us and our clients.",
            "impact":"0",
            "sender":"5",
            "type":"comment",
            "date":"2016-05-19 17:18:50",
            "senderName":"Clint Carlos",
            "senderId":"5",
            "senderTitle":"CEO",
            "senderImgTime":"1450201931",
            "actionName":"Be adventurous and take risks",
            "behavior":"10",
            "likes":[  

            ],
            "replies":[  

            ],
            "totalLikes":"0",
            "recipients":[  
               {  
                  "id":"1053",
                  "name":"Vignesh Krish",
                  "title":"Software Developer Intern",
                  "imgTime":"1462805814"
               }
            ]
         },
         {  
            "id":"442",
            "action":"89",
            "message":"I love how you guys are making progress in week 1 with almost no direction. Great work getting after it!<br><br><img class=\"gifImg\" src=\"https:\/\/media2.giphy.com\/media\/mXnO9IiWWarkI\/giphy.gif\">",
            "impact":"0",
            "sender":"2",
            "type":"comment",
            "date":"2016-05-13 10:05:06",
            "senderName":"Nick Helton",
            "senderId":"2",
            "senderTitle":"CTO",
            "senderImgTime":"1442506570",
            "actionName":"Killing It First Week",
            "behavior":"10",
            "likes":[  
               {  
                  "user":"1038",
                  "name":"Craig Spilker",
                  "score":"1"
               }
            ],
            "replies":[  

            ],
            "totalLikes":"1",
            "recipients":[  
               {  
                  "id":"1052",
                  "name":"Alec Skinner",
                  "title":"Developer Intern",
                  "imgTime":"1465849319"
               },
               {  
                  "id":"1053",
                  "name":"Vignesh Krish",
                  "title":"Software Developer Intern",
                  "imgTime":"1462805814"
               }
            ]
         },
         {  
            "id":"441",
            "action":"67",
            "message":"Alec and Vignesh, you guys in 4 short days have already shown me your talents. &nbsp;Alec - the universal search bar is impressive.<div><br><\/div><div>Vignesh, your work on our app already is really exciting. &nbsp;Can't wait for your talents to be on showcase more and more!<\/div><img class=\"gifImg\" src=\"https:\/\/media4.giphy.com\/media\/3o6ozmKlNQIxQfUf7y\/giphy.gif\">",
            "impact":"0",
            "sender":"1038",
            "type":"comment",
            "date":"2016-05-13 08:50:22",
            "senderName":"Craig Spilker",
            "senderId":"1038",
            "senderTitle":"VP Business Development and Sales",
            "senderImgTime":"1441129118",
            "actionName":"Deliver famous and impactful work",
            "behavior":"8",
            "likes":[  
               {  
                  "user":"1038",
                  "name":"Craig Spilker",
                  "score":"1"
               }
            ],
            "replies":[  

            ],
            "totalLikes":"1",
            "recipients":[  
               {  
                  "id":"1052",
                  "name":"Alec Skinner",
                  "title":"Developer Intern",
                  "imgTime":"1465849319"
               },
               {  
                  "id":"1053",
                  "name":"Vignesh Krish",
                  "title":"Software Developer Intern",
                  "imgTime":"1462805814"
               }
            ]
         },
         {  
            "id":"439",
            "action":"71",
            "message":"Welcome to the Team Vignesh. Can't wait to watch you learn, teach, grow, and SUCCEED with our AMPT Family:)",
            "impact":"0",
            "sender":"3",
            "type":"comment",
            "date":"2016-05-11 13:36:26",
            "senderName":"Collin Caneva",
            "senderId":"3",
            "senderTitle":"Co-founder",
            "senderImgTime":"1393864410",
            "actionName":"Be contagiously positive",
            "behavior":"9",
            "likes":[  
               {  
                  "user":"1038",
                  "name":"Craig Spilker",
                  "score":"1"
               },
               {  
                  "user":"1053",
                  "name":"Vignesh Krish",
                  "score":"1"
               }
            ],
            "replies":[  
               {  
                  "message":"Thank you Collin!:-)",
                  "date":"2016-05-11 15:15:16",
                  "senderName":"Vignesh Krish",
                  "senderId":"1053"
               }
            ],
            "totalLikes":"2",
            "recipients":[  
               {  
                  "id":"1053",
                  "name":"Vignesh Krish",
                  "title":"Software Developer Intern",
                  "imgTime":"1462805814"
               }
            ]
         }
      ]
   }
}

    this is my request manager

    class MyRecognitionRequestManager{

        func parseJson() -> MyRecognitionHomeModel {

            var commentValue = MyRecognitionHomeModel()

            let urlPath = "*******************"
            let url = NSURL(string: urlPath)
            let data = NSData(contentsOfURL: url!)

                do {
                    let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? NSDictionary
                    let comment = jsonData!.objectForKey("comments") as? NSDictionary
                    commentValue = MyRecognitionHomeModel(jsonData: comment!)
                    }

                catch{
                    print("Something went wrong while parsing json data fetched from the API")
            }

            return commentValue

        }

    }


    //model properties class

    import Foundation
    class MyRecognitionHomeModelProperties {

        //MARK: - properties
        var id: String? = "id"
        var message: String? = "message"
        var senderName: String? = "senderName"
        var actionName: String? = "actionName"
        var behavior: String? = "behavior"

        init(jsonData: NSDictionary){
            id = jsonData.objectForKey("id") as? String ?? "id"
            message = jsonData.objectForKey("message") as? String ?? "message"
            senderName = jsonData.objectForKey("senderName") as? String ?? "senderName"
            actionName = jsonData.objectForKey("actionName") as? String ?? "actionName"
            behavior = jsonData.objectForKey("behavior") as? String ?? "behavior"

        }

        init(id: String,message: String, senderName: String, actionName: String,behavior: String) {
            self.id = id
            self.message = message
            self.senderName = senderName
            self.actionName = actionName
            self.behavior = behavior
        }

        init() {
            id = nil
            message = nil
            senderName = nil
            actionName = nil
            behavior = nil
        }

        //prints object's UserInformation
        var description: String {
            return "id:\(id),message: \(message),senderName: \(senderName), actionName: \(actionName),behavior: \(behavior)"

        }

    }

    //model
    import Foundation
    class MyRecognitionHomeModel{

         var commentResults = [MyRecognitionHomeModelProperties]()

        init(jsonData: NSDictionary){

           if let comments = jsonData.objectForKey("comments") as? [NSDictionary] {
                for comment in comments {
                    let commentValue = MyRecognitionHomeModelProperties(jsonData: comment)
                    commentResults.append(commentValue)
                }
            }
        }
    }

This is my tableViewController class
    import UIKit

    class MyRecognitionTableViewController: UITableViewController {

         let requestManager = MyRecognitionRequestManager()
         var commentValue = MyRecognitionHomeModel()


        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.reloadData()
            commentValue = requestManager.parseJson()

            // Uncomment the following line to preserve selection between presentations
            // self.clearsSelectionOnViewWillAppear = false

            // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
            // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        // MARK: - Table view data source

        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            // #warning Incomplete implementation, return the number of sections
            return 1
        }

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            // #warning Incomplete implementation, return the number of rows
            return commentValue.count

        }


        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
            let cell = tableView.dequeueReusableCellWithIdentifier("MyRecognitionCell", forIndexPath: indexPath) as!  MyRecognitionTableViewCell

            // Configure the cell...
            cell.senderName?.text = commentValue.senderName
            cell.actionName?.text = commentValue.actionName
            cell.message?.text = commentValue.message
            return cell
        }


     }

How do I fix the error I am getting? I don't get why there is a missing parameter jsonData in call

2 个答案:

答案 0 :(得分:0)

MyRecognitionHomeModel只有一个初始化程序,而且需要jsonData参数。

答案 1 :(得分:0)

试试这个,如果它适合你,让我知道。

class MyRecognitionRequestManager{

func parseJson() -> MyRecognitionHomeModel {

    let commentValue = MyRecognitionHomeModel()

    let urlPath = "*******************"
    let url = NSURL(string: urlPath)
    let data = NSData(contentsOfURL: url!)

    do {
        if let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? NSDictionary {
            if let comment = jsonData.objectForKey("comments") as? NSDictionary {
                commentValue.jsonData = comment
            }
        }
    }

    catch{
        print("Something went wrong while parsing json data fetched from the API")
    }

    return commentValue

}

}


//model properties class

import Foundation
class MyRecognitionHomeModelProperties {

//MARK: - properties
var id: String? = "id"
var message: String? = "message"
var senderName: String? = "senderName"
var actionName: String? = "actionName"
var behavior: String? = "behavior"

init(jsonData: NSDictionary){
    id = jsonData.objectForKey("id") as? String ?? "id"
    message = jsonData.objectForKey("message") as? String ?? "message"
    senderName = jsonData.objectForKey("senderName") as? String ?? "senderName"
    actionName = jsonData.objectForKey("actionName") as? String ?? "actionName"
    behavior = jsonData.objectForKey("behavior") as? String ?? "behavior"

}

init(id: String,message: String, senderName: String, actionName: String,behavior: String) {
    self.id = id
    self.message = message
    self.senderName = senderName
    self.actionName = actionName
    self.behavior = behavior
}

init() {
    id = nil
    message = nil
    senderName = nil
    actionName = nil
    behavior = nil
}

    //prints object's UserInformation
    var description: String {
        return "id:\(id),message: \(message),senderName: \(senderName), actionName: \(actionName),behavior: \(behavior)"

    }

}

class MyRecognitionHomeModel{

var commentResults = [MyRecognitionHomeModelProperties]()

var jsonData: NSDictionary? = nil {
    didSet {
        if let data = jsonData {
            if let comments = data.objectForKey("comments") as? [NSDictionary] {
                for comment in comments {
                    let commentValue = MyRecognitionHomeModelProperties(jsonData: comment)
                    commentResults.append(commentValue)
                }
            }
        }
    }
}
}

class MyRecognitionTableViewController: UITableViewController {

let requestManager = MyRecognitionRequestManager()
var commentValue = MyRecognitionHomeModel()


override func viewDidLoad() {
    super.viewDidLoad()
    tableView.reloadData()
    commentValue.jsonData = requestManager.parseJson().jsonData

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return commentValue.commentResults.count

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCellWithIdentifier("MyRecognitionCell", forIndexPath: indexPath) as!  MyRecognitionTableViewCell

    // Configure the cell...
    cell.senderName?.text = commentValue.senderName
    cell.actionName?.text = commentValue.actionName
    cell.message?.text = commentValue.message
    return cell
}
}