Swift 更改嵌套字典键的值

Swift 更改嵌套字典键的值,swift,dictionary,nested,Swift,Dictionary,Nested,我有以下变量,我需要用用户输入的值更改/更新例如键“end”的“dateTime”值 var calendarEvent = [ "subject": "Let's go for lunch", "body": [ "contentType": "HTML", "content": "Does late morning work for you?", ], "st

我有以下变量,我需要用用户输入的值更改/更新例如键“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”这个词替换这个值


如何做到这一点?

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

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"
   }
*/

注意变异的主题,从“Let's go for午餐”改为“yea boi”。

使用为您的程序设计的数据类型,而不是字典。我需要向api发布JSON。字典将在所需的类中格式化为JSON。我只需要知道如何更新字典中某个键的值。请使用为您的程序设计的数据类型,并可以序列化为JSON。如果您使用的是Swift 4,非常感谢您快速、易懂的回复!我一有时间就要试试这个!再次感谢大家!关键在于,只要
结构的每个成员都遵循
可编码
或是原语(默认情况下遵循
可编码
),那么整个
结构就可以遵循
可编码
,而无需任何附加代码。另外,记住接受最正确的答案(不仅仅是向上投票),这样其他人就可以看到对你有用的答案。