iOS 问答集合

iOS 如何实现自定义对象(模型)的copy\mutableCo

2018-05-03  本文已影响49人  船长_

场景需求:收到一条消息后,需要添加这条消息前,先插入一条自定义的消息,仅部分字段数据需要手动更改

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
    }
}
上一篇下一篇

猜你喜欢

热点阅读