如何在 Swift 字典内的字典数组中插入键值对

How to insert a key value pair in an array of dictionary which is inside a dictionary in Swift

提问人:Bappaditya 提问时间:4/25/2022 最后编辑:Bappaditya 更新时间:4/25/2022 访问量:134

问:

我们有一个 JSON 响应,应该在保存到 Coredata 之前进行解析,但是我们想在保存之前解析它并进行一些修改。

[{
        "mailId": "316054287",
        "attachedDocumentCount": 3,
        "attachmentFileSize": 2932927,
        "confidential": false,
        "correspondenceType": "Transmittal",
        "mailNo": "AFDE-TRANSMIT-058698",
        "toStatusId": "3",
        "corrTypeId": "23",
        "reasonForIssue": "Client \"Proceed Subject ot Conditions, Revise and Resubmit\"",
        "referenceNumber": "Rebar-TRANSMIT-003389",
        "sentDate": "2022-04-21T15:02:42.637Z",
        "status": "N/A",
        "subject": "C2 - BCH Transmittal - Rebar Shop Drawings SW-EL-CH-07A & 07B - Rev 1 - Review Complete",
        "recipients": {
            "to": [
                {
                    "organizationId": "268497899",
                    "organizationName": "AFDE",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Tony",
                    "name": "Mr Tony Fournier",
                    "userId": "268832810",
                    "lastName": "Fournier",
                    "isRead": true
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Melanie",
                    "name": "Mrs Melanie Coffey",
                    "userId": "268841518",
                    "lastName": "Coffey",
                    "isRead": false
                },
                {
                    "organizationId": "268497899",
                    "organizationName": "AFDE",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Caroline",
                    "name": "Ms Caroline Rancourt",
                    "userId": "268842732",
                    "lastName": "Rancourt",
                    "isRead": true
                },
                {
                    "organizationId": "268497899",
                    "organizationName": "AFDE",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Mathieu",
                    "name": "Mr Mathieu Forcier",
                    "userId": "268844353",
                    "lastName": "Forcier",
                    "isRead": true
                },
                {
                    "organizationId": "268497899",
                    "organizationName": "AFDE",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Laurent",
                    "name": "Mr Laurent Gervais",
                    "userId": "268848608",
                    "lastName": "Gervais",
                    "isRead": true
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Ken",
                    "name": "Mr Ken Trinidad",
                    "userId": "268851135",
                    "lastName": "Trinidad",
                    "isRead": false
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Brian",
                    "name": " Brian Webster",
                    "userId": "268857121",
                    "lastName": "Webster",
                    "isRead": false
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Diana",
                    "name": " Diana Harrison",
                    "userId": "268863136",
                    "lastName": "Harrison",
                    "isRead": false
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Dan",
                    "name": " Dan Lavallee",
                    "userId": "268874809",
                    "lastName": "Lavallee",
                    "isRead": false
                },
                {
                    "organizationId": "268497899",
                    "organizationName": "AFDE",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Cole",
                    "name": "Mr Cole Daniels",
                    "userId": "268941632",
                    "lastName": "Daniels",
                    "isRead": true
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "James",
                    "name": " James Cross",
                    "userId": "268968788",
                    "lastName": "Cross",
                    "isRead": false
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "William",
                    "name": "Mr William Mint",
                    "userId": "268968789",
                    "lastName": "Mint",
                    "isRead": false
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Greg",
                    "name": "Mr Greg Smith",
                    "userId": "268968792",
                    "lastName": "Smith",
                    "isRead": false
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Andrew",
                    "name": "Mr Andrew Rayfield",
                    "userId": "268978580",
                    "lastName": "Rayfield",
                    "isRead": true
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Daniel",
                    "name": "Mr Daniel Sanderson",
                    "userId": "269015696",
                    "lastName": "Sanderson",
                    "isRead": false
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Vic",
                    "name": " Vic Levinsky",
                    "userId": "269017288",
                    "lastName": "Levinsky",
                    "isRead": false
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Greg",
                    "name": " Greg Tait",
                    "userId": "269029103",
                    "lastName": "Tait",
                    "isRead": false
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Brook",
                    "name": " Brook Alling",
                    "userId": "269033767",
                    "lastName": "Alling",
                    "isRead": false
                },
                {
                    "organizationId": "268497899",
                    "organizationName": "AFDE",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Julia",
                    "name": "Ms Julia Brin",
                    "userId": "269060417",
                    "lastName": "Brin",
                    "isRead": true
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Kevin",
                    "name": "Mr Kevin Demattos",
                    "userId": "269097068",
                    "lastName": "Demattos",
                    "isRead": false
                },
                {
                    "organizationId": "268446975",
                    "organizationName": "Harris Rebar",
                    "status": "N/A",
                    "statusId": "3",
                    "distributionType": "TO",
                    "firstName": "Shannon",
                    "name": "Ms Shannon Rochon",
                    "userId": "269125638",
                    "lastName": "Rochon",
                    "isRead": false
                }
            ],
            "cc": [],
            "bcc": []
        },
        "senderDetails": {
            "organizationId": "268497899",
            "organizationName": "AFDE",
            "firstName": "Neil",
            "name": "Mr Neil Cantwell",
            "userId": "269161169",
            "lastName": "Cantwell"
        },
        "mailBoxType": "inbox",
        "hasAttachments": true,
        "myMailsOnly": true,
        "isReadByMe": false
    }]

在这里,我们想从每个邮件对象的顶部键值对中插入,并将其添加到每个收件人对象中,如下所示,mailIdO(1)O(n)

{
                "mailId": "316054287",
                "organizationId": "268446975",
                "organizationName": "Harris Rebar",
                "status": "N/A",
                "statusId": "3",
                "distributionType": "TO",
                "firstName": "Shannon",
                "name": "Ms Shannon Rochon",
                "userId": "269125638",
                "lastName": "Rochon",
                "isRead": false
            }

我们尝试的代码是同谋的。解析和保存数据大约需要 50 秒。我们希望减少这种情况。O(n2)

    // json is saved in `mailResponse` variable
    public typealias JSON = [String: Any]
    guard let mails = mailResponse["mails"] as? [JSON] else {
        return nil
    }
    
    if #available(iOS 14.0, *) { 
        _ = mails.map { mail in
            if let recipients = mail["recipients"] as? JSON {
                
                var allRecipients: [JSON] = [JSON]()
                
                if let toRecipients = recipients["to"] as? [JSON] {
                    allRecipients.append(contentsOf: toRecipients)
                }
                if let ccRecipients = recipients["cc"] as? [JSON] {
                    allRecipients.append(contentsOf: ccRecipients)
                }
                if let bccRecipients = recipients["bcc"] as? [JSON] {
                    allRecipients.append(contentsOf: bccRecipients)
                }
                allRecipients = allRecipients.map({ recipient in
                    var mRecipient = recipient
                    mRecipient["mailId"] = mail["mailId"]
                    return mRecipient
                })
            }
        }
    }

对此的任何帮助都非常感谢。

iOS JSON Swift 解析 时间复杂度

评论

0赞 Larme 4/25/2022
你在上下文中保存在那里吗?我看不出你是否保存了或者它是否是隐藏的代码,但如果你保存了多次,你应该只保存一次。
0赞 Bappaditya 4/25/2022
这些是关系,但是问题在于解析邮件 ID 并将其放在每个收件人对象中,复杂性更高
1赞 Joakim Danielson 4/25/2022
您最新的编辑有点令人困惑,您说解析和保存需要 50 秒,但现在问题中的代码是在解析之后和保存之前执行的,所以真的是包含的代码才是问题吗?
0赞 Bappaditya 4/26/2022
我已经删除了保存部分,因为大多数代码都超出了范围。因此,我们关心的是解析响应并对其进行处理。
1赞 Larme 4/26/2022
我看不出有什么理由要花那么多时间。你能检查一下这真的花了那么多时间吗?只有顶级?你能给其他人计时吗?另外为什么有返回值?你到底在用哪一个?mails.map()mapmap

答: 暂无答案