1
NBY OP emmm
由于没啥人解答 我动了动脑子 试出了这个办法 刚学 swift 和 swiftui 3 天 代码水平一言难尽 希望大家多提意见 pwnedInfo: { data = "Passwords, Email addresses"; date = "2017-04-15"; title = Youku; url = "https://monitor.firefox.com/breach-details/Youku"; }, { data = "Email addresses"; date = "2016-06-30"; title = Tianya; url = "https://monitor.firefox.com/breach-details/Tianya"; }, { data = "Passwords, IP addresses"; date = "2015-10-26"; title = 000webhost; url = "https://monitor.firefox.com/breach-details/000webhost"; } var pwnedInfo: NSMutableArray = [] var pwnedInfoDict: [String: [String: String]] = [:] var id: Int = 0 for i in pwnedInfo { pwnedInfoDict[String(id)] = (i as? [String: String]) id = id + 1 } swiftUI 相关代码: struct showInfo: View { @Binding var popoverIsShown: Bool var body: some View { let keys = pwnedInfoDict.map { $0.key } let values = pwnedInfoDict.map { $0.value } List { ForEach(keys.indices) { index in // var x = print((values[index])) let title = (values[index]["title"]!) if values[index]["date"] != nil { let date = (values[index]["date"]!) } if values[index]["data"] != nil { let data = (values[index]["data"]!) } let url = (values[index]["url"]!) } } } } |
2
xtinput 2021-11-29 04:21:57 +08:00
···· json 转模型呀···· swift 原生的或者三方的,很多成熟的框架
|
4
ccdjh 2021-11-30 01:24:07 +08:00
```json
{ "userData": "[email protected]", "pwnedCount": "3", "pwnedInfo": [ {"title": "Youku", "url": "https://monitor.firefox.com/breach-details/Youku", "date": "2017-04-15", "data": "Passwords, Email addresses" }, {"title": "Tianya", "url": "https://monitor.firefox.com/breach-details/Tianya", "date": "2016-06-30", "data": "Email addresses" }, {"title": "000webhost", "url": "https://monitor.firefox.com/breach-details/000webhost", "date": "2015-10-26", "data": "Passwords, IP addresses"} ], "returnCode": 200 } ``` ```swift //Model 解析 JSON 数据 struct Ccdjh: Codable,Hashable { var userData : String? var pwnedCount : String? var returnCode:String? var pwnedInfo:[CcdjhPwnedInfo]? } struct CcdjhPwnedInfo: Codable,Hashable { var title : String? var url : String? var date:String? var data:String? } //Test 数据 let p1 = CcdjhPwnedInfo( title: "Youku", url: "https://monitor.firefox.com/breach-details/Youku", date: "2017-04-15", data: "Passwords, Email addresses" ) let p2 = CcdjhPwnedInfo( title: "Tianya", url: "https://monitor.firefox.com/breach-details/Tianya", date: "2016-06-30", data: "Email addresses" ) let p3 = CcdjhPwnedInfo( title: "000webhost", url: "https://monitor.firefox.com/breach-details/000webhost", date: "2015-10-26", data: "Passwords, IP addresses" ) let Cc = Ccdjh( userData: "[email protected]", pwnedCount: "3", returnCode: 200, pwnedInfo: [p1,p2,p3] ) //使用数据 Text("\(Cc.userData!)") ``` |