SWIFT使用数组和字典

时间:2018-06-30 12:42:38

标签: arrays json swift dictionary

我正试图通过数组中的segue将信息传递给第二个控制器:

EPERM

这是我实现方法 prepare(用于segue:的样子)

var gists = [Gists]()

这是我的数组要点的转储:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "detailSegue" {
            if let indexPath = tableView.indexPathForSelectedRow {
                 guard let dvc = segue.destination as? DetailViewController else { return }
                dvc.filename = gists[indexPath.row].files.key // type String
                dvc.rawUrl = gists[indexPath.row].files.value // type String
            }
        }
    }

也就是说,我有一组元素要点,每个元素由一个字符串和一个字典组成。反过来,此字典的类型为 [String:String] 。而且我需要传递密钥和相应的值。

Xcode在这些行上产生错误。如何正确书写?

▿ 2 elements
  ▿ GistShowApp.Gists
    ▿ description: Optional("mySecondGist")
      - some: "mySecondGist"
    ▿ files: 1 key/value pair
      ▿ (2 elements)
        - key: "gistfile1.txt"
        ▿ value: GistShowApp.DetailGist
          ▿ filename: Optional("gistfile1.txt")
            - some: "gistfile1.txt"
          ▿ rawUrl: Optional("https://gist.githubusercontent.com/VladimirKhuraskin/9ca2362c09cebcc16bd74f51f267231a/raw/74caacd3ad3eedb369a07b926327d2ef37e3eefc/gistfile1.txt")
            - some: "https://gist.githubusercontent.com/VladimirKhuraskin/9ca2362c09cebcc16bd74f51f267231a/raw/74caacd3ad3eedb369a07b926327d2ef37e3eefc/gistfile1.txt"
  ▿ GistShowApp.Gists
    ▿ description: Optional("helloWorldDemo")
      - some: "helloWorldDemo"
    ▿ files: 1 key/value pair
      ▿ (2 elements)
        - key: "gistfile1.txt"
        ▿ value: GistShowApp.DetailGist
          ▿ filename: Optional("gistfile1.txt")
            - some: "gistfile1.txt"
          ▿ rawUrl: Optional("https://gist.githubusercontent.com/VladimirKhuraskin/c510014b85a6bdfa302f1f3837893a7c/raw/4648e701849ee7d52fb685111a7f0e4323505a35/gistfile1.txt")
            - some: "https://gist.githubusercontent.com/VladimirKhuraskin/c510014b85a6bdfa302f1f3837893a7c/raw/4648e701849ee7d52fb685111a7f0e4323505a35/gistfile1.txt"
2018-06-30 15:30:26.336720+0400 GistShowApp[1889:720551] [Snapshotting] Snapshotting a view (0x134032c00, UIKeyboardImpl) that is not in a visible window requires afterScreenUpdates:YES.

1 个答案:

答案 0 :(得分:0)

gists[indexPath.row].files是一本字典,因此您需要将dvc.filenamedvc.rawUrl设置为该字典中的一个条目,或将整个字典传递给详细视图控制器。

第一个选项:

dvc.filename = gists[indexPath.row].files["gistfile1.txt"].filename
dvc.rawUrl = gists[indexPath.row].files["gistfile1.txt"].rawURL