스위프트에선 NSCoping 인터페이스를 사용하여 쉽게 구현할 수 있다
protocol ProductPrototype: NSCopying {
var id: String { get }
var name: String { get }
var price: Int { get set }
var image: String { get }
var stock: Int { get }
var event: String { get set }
}
class Product: ProductPrototype {
let id: String
let name: String
var price: Int
let image: String
let stock: Int
var event: String
init(id: String, name: String, price: Int, image: String, stock: Int, event: String) {
self.id = id
self.name = name
self.price = price
self.image = image
self.stock = stock
self.event = event
}
func copy(with zone: NSZone? = nil) -> Any {
return Product(
id: id,
name: name,
price: price,
image: image,
stock: stock,
event: event
)
}
}
struct Store {
let product: ProductPrototype = Product(id: "1", name: "칸쵸", price: 1000, image: "", stock: 10, event: "")
func 백원딜() -> ProductPrototype {
let 백원딜상품 = product.copy() as! ProductPrototype
백원딜상품.price = 100
백원딜상품.event = "백원딜"
return 백원딜상품
}
func 천원딜상품() -> ProductPrototype {
let 천원딜상품 = product.copy() as! ProductPrototype
천원딜상품.price = 1000
천원딜상품.event = "천원딜"
return 천원딜상품
}
}