class SocketMsgBody {
var body : AnyObject
}
public class SocketMsg : NSObject,Codable {
var msgType : SocketMsgType
var msgBody : SocketMsgBody
enum CodingKeys: String, CodingKey {
case msgType = "msgType"
case msgBody = "msgBody"
}
enum NestedCodingKeys: String, CodingKey {
case body
}
public init(with type:SocketMsgType) {
msgType = type
msgBody = SocketMsgBody(for: msgType)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(msgType, forKey: .msgType)
var socketBodyInfo = container.nestedContainer(keyedBy: NestedCodingKeys.self, forKey: .msgBody)
try socketBodyInfo.encode(msgBody, forKey: .body)
}
public func serialize() throws -> String? {
let encoder = JSONEncoder()
guard let data = try? encoder.encode(self) else { return nil }
let str = String(data: data, encoding: .utf8)
return str
}
}
我也想对msgbody进行编码,该怎么做?