V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NBY
V2EX  ›  Swift

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

  •  
  •   NBY · 2021-11-28 18:09:16 +08:00 · 1812 次点击
    这是一个创建于 1116 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大佬们好!
    最近我刚开始学 swift 开发,为了练练手打算做个输入 email 地址查询自己的密码在哪些网站泄露的 app
    https://monitor.firefox.com/
    就是简单的用 python 写了个 api 请求上面那个网站 然后返回 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 处理 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 讨论群
    感谢大佬们解答
    4 条回复    2021-11-30 01:24:07 +08:00
    NBY
        1
    NBY  
    OP
       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
        2
    xtinput  
       2021-11-29 04:21:57 +08:00
    ···· json 转模型呀···· swift 原生的或者三方的,很多成熟的框架
    NBY
        3
    NBY  
    OP
       2021-11-29 12:18:45 +08:00
    @xtinput 刚开始学 模型都没接触呢。。。
    ccdjh
        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!)")

    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5730 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 03:09 · PVG 11:09 · LAX 19:09 · JFK 22:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.