URLSession的Data任务在我尝试使对象符合Identifiable协议时返回Nil。

huangapple go评论81阅读模式
英文:

URLSession's Data Task returning Nil when I try to conform the object to Identifiable protocol

问题

I'm trying to run a simple http request to Spotify's api to get the recent played tracks

  1. "https://api.spotify.com/v1/me/player/recently-played?limit=2&before=1676369852305"

Everything works and I do get the result back, but when I add the Identifiable protocol with id variable to my struct objects the decoder fails (nil).
(I want to conform to Identifiable because I need to use the data in a List/ForEach loop

Here's my code

  1. //objects
  2. struct Recent: Codable { //or Codable, Identifiable
  3. //let id: UUID?
  4. let items: [Item]?
  5. let next: String?
  6. let limit: Int?
  7. let href: String?
  8. }
  9. struct Item: Codable { //or Codable, Identifiable
  10. // let id: UUID?
  11. let track: Track?
  12. let played_at: String?
  13. }
  14. struct Track: Codable { //or Codable, Identifiable
  15. //let id: String?
  16. let name: String?
  17. let popularity: Int?
  18. let trackNumber: Int?
  19. let type, uri: String?
  20. }
  21. //function
  22. func getRecentPlayed(miliTime: String) {
  23. //let miliTime = "1676369852305"
  24. let urlString = "https://api.spotify.com/v1/me/player/recently-played?limit=10&before=\(miliTime)"
  25. let url = URL(string: urlString)
  26. let urlSessionConfig = URLSessionConfiguration.default
  27. urlSessionConfig.httpAdditionalHeaders = ["Authorization": "Bearer \(OAuthToken)"]
  28. let urlSession = URLSession(configuration: urlSessionConfig)
  29. let sessionTask = urlSession.dataTask(with: url!) { data, response, error in
  30. if (error == nil && data != nil) {
  31. let jsondecoder = JSONDecoder()
  32. let recentPlayedData = try? jsondecoder.decode(Recent.self, from: data!)
  33. print (recentPlayedData)
  34. recent = recentPlayedData
  35. } else {
  36. print (error)
  37. }
  38. }
  39. sessionTask.resume()
  40. }

What I want:
Run the following loop in swiftui but it screams saying

  1. "Referencing initializer on 'ForEach' requires that 'Item' conform to 'Identifiable"
  1. //swiftui
  2. @State var recent: Recent?
  3. if let r = recent {
  4. NavigationView {
  5. List {
  6. ForEach(r.items!) { item in
  7. Text((item.track?.name)!)
  8. }
  9. }
  10. .navigationTitle("Recently played")
  11. }
  12. }

Any idea anyone?

英文:

I'm trying to run a simple http request to Spotify's api to get the recent played tracks

  1. "https://api.spotify.com/v1/me/player/recently-played?limit=2&before=1676369852305"

Everything works and I do get the result back, but when I add the Identifiable protocol with id variable to my struct objects the decoder fails (nil).
(I want to conform to Identifiable because I need to use the data in a List/ForEach loop

Here's my code

  1. //objects
  2. struct Recent: Codable { //or Codable, Identifiable
  3. //let id: UUID?
  4. let items: [Item]?
  5. let next: String?
  6. let limit: Int?
  7. let href: String?
  8. }
  9. struct Item: Codable { //or Codable, Identifiable
  10. // let id: UUID?
  11. let track: Track?
  12. let played_at: String?
  13. }
  14. struct Track: Codable { //or Codable, Identifiable
  15. //let id: String?
  16. let name: String?
  17. let popularity: Int?
  18. let trackNumber: Int?
  19. let type, uri: String?
  20. }
  21. //function
  22. func getRecentPlayed(miliTime: String) {
  23. //let miliTime = "1676369852305"
  24. let urlString = "https://api.spotify.com/v1/me/player/recently-played?limit=10&before=\(miliTime)"
  25. let url = URL(string: urlString)
  26. let urlSessionConfig = URLSessionConfiguration.default
  27. urlSessionConfig.httpAdditionalHeaders = ["Authorization": "Bearer \(OAuthToken)"]
  28. let urlSession = URLSession(configuration: urlSessionConfig)
  29. let sessionTask = urlSession.dataTask(with: url!) { data, response, error in
  30. if (error == nil && data != nil) {
  31. let jsondecoder = JSONDecoder()
  32. let recentPlayedData = try? jsondecoder.decode(Recent.self, from: data!)
  33. print (recentPlayedData)
  34. recent = recentPlayedData
  35. } else {
  36. print (error)
  37. }
  38. }
  39. sessionTask.resume()
  40. }

What I want:
Run the following loop in swiftui but it screams saying

  1. "Referencing initializer on 'ForEach' requires that 'Item' conform to 'Identifiable"
  1. //swiftui
  2. @State var recent: Recent?
  3. if let r = recent {
  4. NavigationView {
  5. List {
  6. ForEach(r.items!) { item in
  7. Text((item.track?.name)!)
  8. }
  9. }
  10. .navigationTitle("Recently played")
  11. }
  12. }

Any idea anyone?

答案1

得分: 1

struct Recent: Codable {
var id: String?{
href
}
let items: [Item]?
let next: String?
let limit: Int?
let href: String?
}
struct Item: Codable, Identifiable {
var id: String?{
track?.id
}
let track: Track?
let played_at: String?
}
struct Track: Codable, Identifiable {
var id: String?{
uri
}
let name: String?
let popularity: Int?
let trackNumber: Int?
let type, uri: String?
}

英文:

You can use what you have that is already unique

  1. struct Recent: Codable {
  2. var id: String?{
  3. href
  4. }
  5. let items: [Item]?
  6. let next: String?
  7. let limit: Int?
  8. let href: String?
  9. }
  10. struct Item: Codable, Identifiable {
  11. var id:String?{
  12. track?.id
  13. }
  14. let track: Track?
  15. let played_at: String?
  16. }
  17. struct Track: Codable, Identifiable { //or Codable, Identifiable
  18. var id: String?{
  19. uri
  20. }
  21. let name: String?
  22. let popularity: Int?
  23. let trackNumber: Int?
  24. let type, uri: String?
  25. }

答案2

得分: 0

以下是翻译好的部分:

  1. struct Track: Codable, Identifiable {
  2. let id = UUID() //或者其他内容,不清楚你的id是什么
  3. let name: String?
  4. let popularity: Int?
  5. let trackNumber: Int?
  6. let type, uri: String?
  7. }
英文:

It is not possible exactly as you want, I think that the closest thing to what you want is this:

  1. struct Track: Codable, Identifiable{ //or
  2. let id = UUID() //or something else, it is not clear what is your id
  3. let name: String?
  4. let popularity: Int?
  5. let trackNumber: Int?
  6. let type, uri: String?
  7. }

huangapple
  • 本文由 发表于 2023年2月14日 22:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定