如何将字典输出显示到UIText视图或UITableview中?我失去了了解

时间:2018-09-11 15:26:54

标签: arrays dictionary uitextview swift4

我在Swift 4中有以下代码,并且可以在一条打印语句中很好地输出信息,但是我很难理解如何在UITextfield中显示它-不断出现错误,如注释部分所示。 我正在努力理解它是如何工作的,字典和数组等,并且可能需要一些帮助。

                let index1 = todo.index(forKey: "ledger")
    print("Stored Key: \(todo[index1!].key)")
    let btn_ledger = (todo[index1!].value)
            DispatchQueue.main.async {
                print("Ledger String: \(todo[index1!].value)")
                if let ledgerString = todo[index1!].value as? String {
                    self.displayLedger.text = ledgerString
                } else {
                    // Ooops
                    self.displayLedger.text = "?? unknown ??"
                }
            }
            print("myLedger I: \(btn_ledger)")
            //self.displayLedger.text = (btn_ledger)
            //Could not cast value of type '__NSDictionaryI' (0x10ae73508) to 'NSString' (0x109fe42a8).
        } catch  {
            print("error trying to convert data to JSON")
            return
        }

    }
    task.resume()

我已经在这个论坛上研究了数百个问题,但变得更加困惑。 print语句的输出的每一行都是这样的。

Ledger String: {
0 = "969081.724";
1 = "20016089.67";
10 = 20000300;
11 = 167772;
12 = "22798.006";
13 = 300;
14 = 300;
15 = 46533;
16 = 2;
17 = 64419;
18 = 7284;
19 = 2;
2 = "1140.4";
20 = 2492;
21 = "92276.546";
....
}

您可以看到它没有排序。现在,我可以找到排序函数,可以找到将字典放入数组并对其进行排序的函数,但是找不到解决该问题的方法...

从json获取数据

            do {
            guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
                as? [String: Any] else {
                    print("error trying to convert data to JSON")
                    return
            }

待办事项示例。这是我输入的第一部分代码的开头。

                // now we have the todo
            // let's just print it to prove we can access it
            //print("The todo is: " + todo.description)
            // the todo object is a dictionary
            // so we just access the title using the "title" key
            // so check for a title and print it if we have one
            let index1 = todo.index(forKey: "ledger")

UITextfield是 self.displayLedger.text = ledgerString

但是也许这需要一个TableView吗?那么,在某种描述的文本字段上显示此数据列表的机制是什么?

谢谢

0 个答案:

没有答案
相关问题