【问题标题】:Change value for nested dictionary key [closed]更改嵌套字典键的值 [关闭]
【发布时间】:2018-05-23 16:55:17
【问题描述】:

我有以下 var 我需要更改/更新例如键 'end''dateTime' 值与用户拥有的值放进去。

 var calendarEvent = [
        "subject": "Let's go for lunch",
        "body": [

                "contentType": "HTML",
                "content": "Does late morning work for you?",

        ],
        "start": [

                "dateTime":"2017-12-10T12:55:00",
                "timeZone": "W. Europe Standard Time"

        ],
        "end": [

                "dateTime": "2017-12-10T14:00:00",
                "timeZone": "W. Europe Standard Time"

        ],
        "location": [

                "displayName": "Antwerpen"

        ],
        "attendees": [],
] as [String: AnyObject]

假设我们不关心用户输入 - 因为它只是一个字符串 - 并且只想用单词 'yea boi' 替换该值。

我该怎么做?

【问题讨论】:

  • 使用为您的程序设计的数据类型,而不是字典。
  • 我需要将 JSON 发布到 api。字典在所需的类中格式化为 JSON。
  • 我只需要知道如何更新这个字典中某个键的值。
  • 使用专为您的程序设计并且可以序列化为 JSON 的数据类型。如果你正在使用 Swift 4 this is nearly automatic.

标签: swift dictionary nested


【解决方案1】:

最不容易出错、最简单和最便携的方法是定义结构并使用Codable 协议以及JsonEncoderJsonDecoder 来读取和写入端点的JSON 字符串。然后,如果您需要更改某个key,您可以简单地将其视为任何其他结构并直接更改它。

import Foundation

// structures for encoding/decoding

struct Body: Codable {
  let contentType: String
  let content: String
}

struct Time: Codable {
  let dateTime: String
  let timeZone: String
}

struct Location: Codable {
  let displayName: String
}

struct CalendarEvent: Codable {
  var subject: String // mutable
  let body: Body
  let start: Time
  let end: Time
  let location: Location
  let attendees: [String]
}

// set up structure

var event = CalendarEvent(subject: "Let's go for lunch",
                body: Body(contentType: "HTML",
                            content: "Does late morning work for you?"),
                start: Time(dateTime:"2017-12-10T12:55:00",
                             timeZone: "W. Europe Standard Time"),
                end: Time(dateTime:"2017-12-10T14:00:00",
                           timeZone: "W. Europe Standard Time"),
                location: Location(displayName: "Antwerpen"),
                attendees: [])

// change the subject
event.subject = "yea boi"

// create encoder

let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = [.prettyPrinted, .sortedKeys]

// encode, convert to a String, and print it

if let jsonData = try? jsonEncoder.encode(event),
  let jsonString = String(data: jsonData, encoding: .utf8) {
  print(jsonString)
}

// output

/* {
     "attendees" : [],
     "body" : {
       "content" : "Does late morning work for you?",
       "contentType" : "HTML"
     },
     "end" : {
       "dateTime" : "2017-12-10T14:00:00",
       "timeZone" : "W. Europe Standard Time"
     },
     "location" : {
       "displayName" : "Antwerpen"
     },
     "start" : {
       "dateTime" : "2017-12-10T12:55:00",
       "timeZone" : "W. Europe Standard Time"
     },
     "subject" : "yea boi"
   }
*/

注意变异的主题,从“我们去吃午饭”变成了“yea boi”。

【讨论】:

  • 非常感谢您快速且易于理解的回复!我一有时间就试试这个!再次,非常感谢!
  • 关键是只要struct的每个成员都遵守Codable或者是一个原语(默认遵守Codable)那么整个struct都可以遵守Codable 没有任何附加代码。另外,请记住接受最正确的答案(不仅仅是点赞),以便其他人可以看到对您有用的答案。
猜你喜欢
  • 2021-04-14
  • 2018-12-01
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多