我正在从文件中读取一个字符串并将其转换为Dictionary但我无法将其加载到tableview中:这是我的代码:
override func viewDidLoad() {
let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = DocumentDirURL.appendingPathComponent("x").appendingPathExtension("json")
filePath = Bundle.main.path(forResource: "x", ofType: "json")
do {
let readString = try String(contentsOf: fileURL)
let x = readString.components(separatedBy: "\n")
do{
}catch{
print (" error nil ")
}
for i in 1..<x.count{
do{
let y = x[i]
var b = convertToDictionary(text: y)
let date = try b?["date"]
dictarr["date"] = "\(date)"
print (" midas \(dictarr["date"]) ")
let response = try b?["response"]!
dictarr["response"] = "\(response)"
// print (" midas \(dictarr["response"]) ")
let message = try b?["message"]
dictarr["message"] = "\(message)"
// print (" midas \(dictarr["message"]) ")
clinetarr.add(dictarr)
}catch{
print (" error nil ")
}
}
} catch let error as NSError {
print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
}
}
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print ("nil found")
//print(error.localizedDescription)
}
}
return nil
}
这是我的表格视图
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let datainrow = clinetarr[indexPath.row]
cell.textLabel?.text = "\((datainrow as AnyObject).object("message")!)"
cell.textLabel?.text=itemListTmp[keysItem[indexPath.section]]?.removeFirst()
// Configure the cell...
return cell
}
在这一行中,cell.textLabel?.text =“((datainrow as AnyObject).object(”message“)!)”它给了我错误:非函数类型的调用值'任何'我的主要目标是在一个单元格内的一行中显示:dictarr [“date”],dictarr [“response”]和dictarr [“message”]。
答案 0 :(得分:0)
您从字典中读取的方式不正确,您应该将每个值转换为正确的类型。我不知道你的价值是什么类型,但如果我们假设它们都是String类型我会做这样的事情
var output: String = ""
if let date = dictarr["date"] as? String {
output = "\(date) " //using space as separator
}
if let response dictarr["response"] as? String {
output.append(response)
output.append(" ")
}
if let message dictarr["message"] as? String {
output.append(message)
}
cell.textLabel?.text = output
答案 1 :(得分:0)
非常感谢@Joakim Danielson
我几小时前就设法解决了这个问题我就是这样做的:
for i in 1..<x.count{
do{
if x != nil {
let y = x[i]
let b = convertToDictionary(text: y)
if let date = try b?["date"]{
dictarr["date"] = "\(date)"
}
if let response = try b?["response"]{
dictarr["response"] = "\(response)"
}
if let message = try b?["message"]{
dictarr["message"] = "\(message)"
}
clinetarr.append(dictarr)
}
}catch{
print (" error nil ")
}
}
这是tableview:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let client = clinetarr[indexPath.row]
cell.detailTextLabel!.numberOfLines = 0
cell.textLabel?.text = client["response"] as! String
cell.detailTextLabel?.text = "\(client["message"] as! String) \n \( client["date"] as! String)"
return cell
}