为什么我的文字没有添加到我的标签中?

时间:2017-09-25 15:34:51

标签: json swift uilabel swift4

修改后的帖子:所以下面发布的代码是我的结果

 struct AnimeJsonStuff: Decodable {
    let data: [AnimeDataArray]
}

struct AnimeLinks: Codable {
    var selfStr   : String?

    private enum CodingKeys : String, CodingKey {
        case selfStr     = "self"
    }
}
struct AnimeAttributes: Codable {
    var createdAt   : String?
    var slug : String?
    private enum CodingKeys : String, CodingKey {
        case createdAt     = "createdAt"
        case slug = "slug"
    }
}
struct AnimeRelationships: Codable {
    var links   : AnimeRelationshipsLinks?

    private enum CodingKeys : String, CodingKey {
        case links     = "links"
    }
}

struct AnimeRelationshipsLinks: Codable {
    var selfStr   : String?
    var related   : String?

    private enum CodingKeys : String, CodingKey {
        case selfStr     = "self"
        case related     = "related"
    }
}

struct AnimeDataArray: Codable {
    let id: String?
    let type: String?
    let links: AnimeLinks?
    let attributes: AnimeAttributes?
    let relationships: [String: AnimeRelationships]?

    private enum CodingKeys: String, CodingKey {
        case id = "id"
        case type = "type"
        case links = "links"
        case attributes = "attributes"
        case relationships = "relationships"
    }
}

此代码是我解析数据的函数:

    func jsonDecoding() {

    let jsonUrlString = "https://kitsu.io/api/edge/anime"

    guard let url = URL(string: jsonUrlString) else {return}
    URLSession.shared.dataTask(with: url) { (data, response, err) in
        guard let data = data else {return}
        do {
            let animeJsonStuff =  try JSONDecoder().decode(AnimeJsonStuff.self, from: data)
            for anime in animeJsonStuff.data {
                //   print(anime.id)
                //    print(anime.type)
                //   print(anime.links?.selfStr)
                let animeName = anime.attributes?.slug
                print(animeName)
                DispatchQueue.main.async {
                    self.nameLabel.text = animeName
                }

                for (key, value) in anime.relationships! {
                    //   print(key)
                    //   print(value.links?.selfStr)
                    //    print(value.links?.related)
                }
            }
        } catch let jsonErr {
            print("Error serializing json", jsonErr)
        }
        }.resume()
}

这是控制台打印出的内容:

Optional("cowboy-bebop")
Optional("cowboy-bebop-tengoku-no-tobira")
Optional("trigun")
Optional("witch-hunter-robin")
Optional("beet-the-vandel-buster")
Optional("eyeshield-21")
Optional("honey-and-clover")
Optional("hungry-heart-wild-striker")
Optional("initial-d-fourth-stage")
Optional("monster")
Optional("cowboy-bebop")
Optional("cowboy-bebop-tengoku-no-tobira")
Optional("trigun")
Optional("witch-hunter-robin")
Optional("beet-the-vandel-buster")
Optional("eyeshield-21")
Optional("honey-and-clover")
Optional("hungry-heart-wild-striker")
Optional("initial-d-fourth-stage")
Optional("monster")
Optional("cowboy-bebop")
Optional("cowboy-bebop-tengoku-no-tobira")
Optional("trigun")
Optional("witch-hunter-robin")
Optional("beet-the-vandel-buster")
Optional("eyeshield-21")
Optional("honey-and-clover")
Optional("hungry-heart-wild-striker")
Optional("initial-d-fourth-stage")
Optional("monster")

它现在显示文本,但它只显示最后一个可选的怪物,而不是所有其他的,当我有三个单元格时。它只在每个细胞中显示怪物。 它应该是

第一单元格:Cowboy-bebpop 第二单元格:cowboy-bebop-tengoku-no-tobira 第三个细胞:trigun 等等

1 个答案:

答案 0 :(得分:0)

  1. 我无法确定您在哪里设置post变量。
  2. 您将nambeLabel放入Controller的视图层次结构中的位置?
  3. 也许你应该在主线程中设置nameLabel.text

    DispatchQueue.main.async {
        self.nameLabel.attributedText = attributedText
    }