1

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key salesDatatext.'

Crashes at upon loading this screen. Is the problem because I'm expecting a mixture of text and integers back, or should I be looking for integers?

I'm lost. Please help. I am new the iOS realm and especially Swift. I apologize for asking a dumb question but just assume I'm slow and will more than likely follow up on your comments with more questions. Please be patient with the noob.

class SalesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableView : UITableView!
    var salesArray = [String]()

    let sales_url = "https://www.testing.com/api/resources/get_films_sales/4464"

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return salesArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "salesData", for:indexPath) as! SalesDataTableViewCell
        // Configuring Cell
        cell.salesData.text = salesArray[indexPath.row]
        // Returning the cell
        return cell
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self

        let url:URL = URL(string: sales_url)!
        let session = URLSession.shared

        let request = NSMutableURLRequest(url: url)
        request.httpMethod = "GET"
        request.setValue("*masked*", forHTTPHeaderField: "X-API-KEY")
        request.setValue("*masked*", forHTTPHeaderField: "Authorization")
        request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
        let paramString = ""

        request.httpBody = paramString.data(using: String.Encoding.utf8)

        let task = session.dataTask(with: request as URLRequest, completionHandler: {
            (
            data, response, error) in

            guard let _:Data = data, let _:URLResponse = response  , error == nil else {

                return
            }



            var json:Any?

            do
            {
                if let existingData = data {
                    json = try JSONSerialization.jsonObject(with: existingData, options: [])
                }

                //  Prasing JSON
                if let parsedData = json as? [[String:Any]] {
                    for dict in parsedData {
                        if let AmazonSales = dict["AmazonSales"] as? String {
                            self.salesArray.append(AmazonSales)
                            print(json)
                        }
                    }

                    OperationQueue.main.addOperation({
                        self.tableView.reloadData()
                    })
                }
            }
            catch
            {
                return
            }

            guard let server_response = json as? NSDictionary else
            {
                return
            }

            if let data_block = server_response["data"] as? NSDictionary
            {
                if let session_data = data_block["session"] as? String
                {
                    //  self.login_session = session_data

                    let preferences = UserDefaults.standard
                    preferences.set(session_data, forKey: "session")

                    //  DispatchQueue.main.async(execute: self.LoginDone)
                }
            }
        })

        task.resume()

        // Do any additional setup after loading the view.
    }

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



}

0 Answers0