来自dictionary的SwiftUI排序列表



我正试图制作一个这样的有序列表:

  1. Lorem ipsum悲哀坐amet
    • consectetur adipiscing elit
    • sed做eiusmod tempor
  2. Nemo enim ipsam volutitem quia
    • 连续卷
    • 威尼斯广告

所以我尝试将ForEach与[String:[String]]、一起使用

但是编译器给了我一个错误";无法为表达式生成诊断;请提交错误报告";

在这个值数组上循环的正确方式是什么?

struct SelectDeviceInfoView: View {
let data: [String: [String]] = [
"1. Check your iOS device settings" : ["Ensure that your iOS device is connected to the same Wifi network as your TV.", "Any “Safe browsing” plugins/apps or VPN proxies on your iOS device will block the detection of TVs. You should disable them.", "Check your phone Wifi settings. Go to 'Settings' > 'Wi-Fi' > Select the connected network (click the blue ⓘ button) > Ensure “Private Address” is turned off.", "Ensure that the app has permission to access devices in your local network. Go to 'Settings' > 'Privacy' > 'Local Network' > Ensure “Screen Mirror” app has the permission."],
"2. Check your router settings" : ["If you're using a multi-band router (e.g. with 2.4 and 5 GHz antennas). So both your TV and iOS device should be on the same band.", "Ensure that you aren’t connected to the public Wifi network; such networks only provide an internet connection, but block all all other internal network traffic. So you can’t connect to a Smart TV.", "Verify that UPNP is enabled in your Router settings.", "Check whether you have multiple subnets in your network. In that case you won’t be able to connect to TV.", "Reboot the router to renew the IP address of your TV.", "Unplug the power cord, wait 1 minute and reconnect the power cord. Also do this for other network devices, such as Wifi repeaters and etc.", "If you have a router with MAC address filtering, add the MAC address of your TV to the list of filtered devices.", "Disable Access Point/Client isolation on your router.", "Disable Wireless Isolation on your router."]]


var body: some View { <-- error here
VStack {
List {
ForEach(data.sorted(using: >), id: .key) { key, value in
Section(header: Text(key)) {
ForEach(value, id: .self) { text in
Text(text)
}
}
}
}
}
}
}

ForEach的数据应该是RandomAccessCollection。但字典不是。

此用例的一个可能的数据结构是Array(符合RandomAccessCollection(。因此,我们必须创建自定义数据数组。

struct MyData: Hashable { // <=here
let title: String
let sortedSubItems: [String]
}
struct ContentView: View {
let data: [String: [String]] = [
"1. Check your iOS device settings" : ["Ensure that your iOS device is connected to the same Wifi network as your TV.", "Any “Safe browsing” plugins/apps or VPN proxies on your iOS device will block the detection of TVs. You should disable them.", "Check your phone Wifi settings. Go to 'Settings' > 'Wi-Fi' > Select the connected network (click the blue ⓘ button) > Ensure “Private Address” is turned off.", "Ensure that the app has permission to access devices in your local network. Go to 'Settings' > 'Privacy' > 'Local Network' > Ensure “Screen Mirror” app has the permission."],
"2. Check your router settings" : ["If you're using a multi-band router (e.g. with 2.4 and 5 GHz antennas). So both your TV and iOS device should be on the same band.", "Ensure that you aren’t connected to the public Wifi network; such networks only provide an internet connection, but block all all other internal network traffic. So you can’t connect to a Smart TV.", "Verify that UPNP is enabled in your Router settings.", "Check whether you have multiple subnets in your network. In that case you won’t be able to connect to TV.", "Reboot the router to renew the IP address of your TV.", "Unplug the power cord, wait 1 minute and reconnect the power cord. Also do this for other network devices, such as Wifi repeaters and etc.", "If you have a router with MAC address filtering, add the MAC address of your TV to the list of filtered devices.", "Disable Access Point/Client isolation on your router.", "Disable Wireless Isolation on your router."]]

var listData: [MyData] { // <=here
data.map {
MyData(title: $0.key, sortedSubItems: $0.value.sorted(by: { $0 < $1 }))
}
.sorted { $0.title < $1.title }
}

var body: some View {
VStack {
List {
ForEach(listData , id: .self) {  myDataItem in
Section(header: Text(myDataItem.title)) {
ForEach(myDataItem.sortedSubItems, id: .self) { sortedSubItem in
Text(sortedSubItem)
}
}
}
}
}
}
}

最新更新