***由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[setValue:forUndefinedKey:]:此类与密钥salesDatatext不符合密钥值编码。'
加载此屏幕时崩溃。问题是因为我期待文本和整数的混合,或者我应该寻找整数?
我迷路了。请帮忙。我是iOS领域的新手,尤其是Swift。我为提出一个愚蠢的问题而道歉,但只是假设我很慢,并且很可能会更多问题跟进你的评论。请耐心等待。
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.
}
}