从嵌套结构 swift ui 创建列表

Posted

技术标签:

【中文标题】从嵌套结构 swift ui 创建列表【英文标题】:Create list from nested struct swift ui 【发布时间】:2021-05-23 21:05:12 【问题描述】:

我正在我的 ios 应用程序中测试一个列表选择器,它允许用户选择一个播放列表。该示例编译但预览不是预期的。从 JSON 文件中,您可以看到每个集合中有不同的数据,但是在我的列表中,它似乎只选择第一个值并针对集合的大小重复它。我的列表中缺少什么,我认为语法是列表(您的集合,标识符:.name of identifier in your struct。它基本上在每个条目上打印相同的索引。

[

    "id" :"1",
"partyinfo": 
"artwork_url": "Rectangle 34",
"id": 1234,
"stream_url": "https://api.soundcloud.com/tracks/1234/stream",
"partyname": "Josh's Party",
"songs": "4 songs",
"participants":"3",
"location":"1010",
"user": 
  "avatar_url": "https://i1.sndcdn.com/avatars-large.jpg",
  "id": 12345,
  "kind": "user",
  "last_modified": "2020/09/03 14:08:25 +0000",
  "permalink": "permalink",
  "permalink_url": "https://soundcloud.com/permalink",
  "uri": "https://api.soundcloud.com/users/12345",
  "username": "Josh",
  "$$ref": "#/components/examples/UserOf/value"


,

    "id" :"2",
"partyinfo": 
"artwork_url": "Rectangle 35",
"id": 1234,
"stream_url": "https://api.soundcloud.com/tracks/1234/stream",
"partyname": "Alan's Party",
"songs": "4 songs",
"participants":"3",
"location":"1010",
"user": 
  "avatar_url": "https://i1.sndcdn.com/avatars-large.jpg",
  "id": 12345,
  "kind": "user",
  "last_modified": "2020/09/03 14:08:25 +0000",
  "permalink": "permalink",
  "permalink_url": "https://soundcloud.com/permalink",
  "uri": "https://api.soundcloud.com/users/12345",
  "username": "Alan",
  "$$ref": "#/components/examples/UserOf/value"



]

派对列表

import SwiftUI

struct PartyList: View 

var partyInfoList: [PartyInfo]

    var b: [PartyInfo] = []
    for n in partylist 
        b.append(n.partyinfo)
        print(n.partyinfo.partyname)
       
    
    return b
 



    
var body: some View 

    
    List(partyInfoList, id: \.id) party in
        Text(party.partyname)
    





struct PartyList_Previews: PreviewProvider 

static var previews: some View 
    PartyList()


模型数据

import Foundation

var tracklist: [Tracklist] = load("tracklist.json")
var partylist: [Party] = load("partylist.json")

final class ModelData: ObservableObject 

//@Published var tracklist: [Tracklist] = load("tracklist.json")
//@Published var partylist: [Party] = load("partylist.json")



func load<T: Decodable>(_ filename: String) -> T 
let data: Data

guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
    else 
    print("error 1")
        fatalError("Couldn't find \(filename) in main bundle.")
    


do 
    data = try Data(contentsOf: file)
 catch 
    print("error 2")
    fatalError("Couldn't load \(filename) from main bundle:\n\(error)")


do 
    let decoder = JSONDecoder()
    print("Successful")
    return try decoder.decode(T.self, from: data)
 catch 
    print("error 3")
    fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")


党的结构

import Foundation
import SwiftUI

struct Party:Hashable, Codable 

let partyinfo: PartyInfo



struct PartyInfo : Hashable, Codable,  Identifiable 

var id: Int
var partyname: String
var songs: String
var participants: String
var location: String


private var artwork_url: String
var image: Image 
    getSafeImage(named: artwork_url)


func getSafeImage(named: String) -> Image 
   let uiImage =  (UIImage(named: named) ?? UIImage(named: "Default.png"))!
   return Image(uiImage: uiImage)


【问题讨论】:

【参考方案1】:

您的 JSON 数据中的两个实体现在具有相同的 id1234

因为您的List 使用.id 来区分项目,所以具有重复ids 的实体可能会发生意想不到的事情。

要解决此问题,请确保您的实体具有唯一的 id 字段。

【讨论】:

菜鸟错误。谢谢!!!。你能用 .id 以外的东西来区分吗? 您可以,尽管使用久经考验的id 可能最容易。在您的ForEachList 中,您可以提供用作id 的密钥路径:id: \.id

以上是关于从嵌套结构 swift ui 创建列表的主要内容,如果未能解决你的问题,请参考以下文章

从嵌套字典结构列表(具有两个级别)创建数据框的 Pythonic 方法是啥?

创建嵌套结构的新列表

swift UI数组创建BUTTON

尝试在颤振中创建嵌套列表视图

带有嵌套导航视图的 Swift UI

使用 Realm 进行 Swift XCTest UI 测试