Swift ui复制和编辑结构可识别编码
我有以下结构:Swift ui复制和编辑结构可识别编码,swift,object,struct,codable,Swift,Object,Struct,Codable,我有以下结构: struct Note: Identifiable, Codable { enum CodingKeys: CodingKey { case title, message, background } let id = UUID() let title: String let message: String let background: NoteBackground func copy(with zone
struct Note: Identifiable, Codable {
enum CodingKeys: CodingKey {
case title, message, background
}
let id = UUID()
let title: String
let message: String
let background: NoteBackground
func copy(with zone: NSZone? = nil) -> Any {
let copy = Note(title: title, message: message, background: background)
return copy
}
}
我希望能够做到的是能够复制和修改一个id不是复制对象的对象
let note = Note(
title: "Title",
message: "Message",
background: .url("https://a.wattpad.com/useravatar/climaxmite.256.718018.jpg")
)
let note2 = note
.copy()//clone
.edit(background: .gradient([
Color(red: 0, green: 0.95, blue: 1),
Color(red: 0.56, green: 0.18, blue: 0.89)
])) as! Note
我该怎么做呢?如果你想要引用类型的行为,为什么不使用
类呢?我不知道你的意思。您的意思是想要一份副本并更改注释的一些变量吗?您想通过使用类来更改原始版本吗?另外,您不需要像以前那样在结构上复制一致性,因此您可以删除函数副本(区域:NSZone?=nil)->任何。只需创建自己的func copy()->Note
即可创建返回类型Note
。您也可以不复制对象,而是改变id
。