swiftui Swift 问题 顺便求推荐 ios 开发的讨论群

2021-11-28 18:09:16 +08:00
 NBY
大佬们好!
最近我刚开始学 swift 开发,为了练练手打算做个输入 email 地址查询自己的密码在哪些网站泄露的 app
https://monitor.firefox.com/
就是简单的用 python 写了个 api 请求上面那个网站 然后返回 json


{"userData": "xxx@xxx.com", "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 处理 pwnedInfo 的时候 发现 pwnedInfo 的数据类型是 NSMutableArray

print 出来是这样的:
{
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";
}

有没有什么简单的方法处理 NSMutableArray ?

想以下面这个样子呈现出来:
https://i.loli.net/2021/11/28/ksVCX1PR8GQ43Um.png
https://i.loli.net/2021/11/28/IisypvJCY2kNoT8.png

最后想问问有没有啥 swift 开发相关的 tg/discord 讨论群
感谢大佬们解答
1761 次点击
所在节点    Swift
4 条回复
NBY
2021-11-29 02:40:04 +08:00
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"]!)
}
}
}
}
xtinput
2021-11-29 04:21:57 +08:00
···· json 转模型呀···· swift 原生的或者三方的,很多成熟的框架
NBY
2021-11-29 12:18:45 +08:00
@xtinput 刚开始学 模型都没接触呢。。。
ccdjh
2021-11-30 01:24:07 +08:00
```json
{
"userData": "xxx@xxx.com",
"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: "xxx@xxx.com",
pwnedCount: "3",
returnCode: 200,
pwnedInfo: [p1,p2,p3]
)

//使用数据
Text("\(Cc.userData!)")

```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/818556

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX