场景需求:收到一条消息后,需要添加这条消息前,先插入一条自定义的消息,仅部分字段数据需要手动更改
let newModel = model.copy() as! ChatModel
newModel.messageContent = "新的消息"
示例代码
extension ChatModel: NSCopying,NSMutableCopying {
func copy(with zone: NSZone? = nil) -> Any {
let model = ChatModel.init()
model.timestamp = self.timestamp
model.messageContent = self.messageContent
return model
}
func mutableCopy(with zone: NSZone? = nil) -> Any {
let model = ChatModel.init()
model.timestamp = self.timestamp
model.messageContent = self.messageContent
return model
}
}