简体   繁体   English

类型“ customDataObject”不符合协议“序列”

[英]Type 'customDataObject' does not conform to protocol 'Sequence'

What I'm trying to do is retrieve json data(which is in array format) and check to see if my local array already contains the data, if it does move on to the next value in the JSON data until their is a value that the array doesn't contain then append it to the array. 我想做的是检索json数据(采用数组格式),并检查本地数组是否已包含该数据,如果它确实移至JSON数据中的下一个值,直到它们是一个数组不包含,然后将其附加到数组。 This data in the array must be in order. 数组中的此数据必须是有序的。 I'm attempting to do this now but get the error: 我现在尝试执行此操作,但收到错误消息:

Type 'ResultsGenrePosters' does not conform to protocol 'Sequence' 类型“ ResultsGenrePosters”不符合协议“序列”

This is what it looks like: 看起来是这样的:

public struct ResultsGenrePosters: Decodable {

  public let results :  [GenrePosters]?

  public init?(json: JSON) {
    results = "results" <~~ json
  }
}


public struct GenrePosters: Decodable {

  public let poster : String

  public init? (json: JSON) {

    guard let poster: String = "poster_path" <~~ json
    else {return nil}
  self.poster = poster
  }

  static func updateGenrePoster(genreID: NSNumber, urlExtension: String, completionHandler:@escaping (_ details: [String]) -> Void){

    var posterArray: [String] = []

    let nm = NetworkManager.sharedManager

    nm.getJSONData(type:"genre/\(genreID)", urlExtension: urlExtension, completion: {
      data in

      if let jsonDictionary = nm.parseJSONData(data)
      {
        guard let genrePosters = ResultsGenrePosters(json: jsonDictionary)

          else {
            print("Error initializing object")
            return
        }
        guard let posterString = genrePosters.results?[0].poster

          else {
            print("No such item")
            return
        }

        for posterString in genrePosters {

          if posterArray.contains(posterString){continue
          } else { posterArray.append(posterString) } //This is where the error happens

        }
      }
      completionHandler(posterArray)
    })
  }

}

Alt + click on genrePosters and what does it tell you? Alt +单击genrePosters,它告诉您什么? It should say its ResultsGenrePosters because thats what the error is saying. 它应该说它的ResultsGenrePosters,因为那是错误的意思。 Now look at the type of posterArray; 现在看看posterArray的类型; its an array of String, not Array ResultsGenrePosters. 它是一个字符串数组,而不是Array ResultsGenrePosters。 I think you mean to write for poster in genrePosters and have confused yourself about the types because you wrote for posterString in genrePosters . 我认为您的意思是在genrePosters中为poster编写代码 ,并对类型感到困惑,因为您在genrePosters中为posterString编写了代码

Maybe you want to use map to transform genrePosters into a [String] ? 也许您想使用map将genrePosters转换为[String]?

This transforms your posterArray, if it exists into an array containing just the poster names. 如果您的posterArray存在,则将其转换为仅包含发布者名称的数组。 If it doesn't exist you get an empty array. 如果不存在,则会得到一个空数组。 This only works if poster is String. 这仅在发帖人为String时有效。 If its String? 如果是String? you should use flatMap instead. 您应该改用flatMap。

let posterNames = genrePosters.results?.map { $0.poster } ?? [String]()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM