I am trying to use swift NSCopy to do a deep copy of a GKGameModel's object, including all players and their wallet reference (containing Integers representing their cash).
Using Swift playgrounds I try to credit all copied players with $100, but leave original player objects intact.
However, I notice that the code impacts original player objects too.
At a high level, the code should show:
Game class, a GKGameModel
Player class, a GKPlayerModel
Wallet class, handles very basic player's Int transactions.
Goals:
Code follows:
// Uses Swift playground
import GameplayKit
class Game : NSObject, GKGameModel{
override var description: String {
return ("game: \(String(describing: self.players))")
}
// -- GKGameModel code follows --
var players: [GKGameModelPlayer]?
var activePlayer: GKGameModelPlayer?
func gameModelUpdates(for player: GKGameModelPlayer) -> [GKGameModelUpdate]? {
return nil
}
func unapplyGameModelUpdate(_ gameModelUpdate: GKGameModelUpdate) {
}
func apply(_ gameModelUpdate: GKGameModelUpdate) {
}
func setGameModel(_ gameModel: GKGameModel) {
guard let inputModel = gameModel as? Game else {
assertionFailure("No game model initialised")
return
}
guard let players = inputModel.players else {
assertionFailure("No players initialised")
return
}
self.activePlayer = inputModel.activePlayer
self.players = players
}
func copy(with zone: NSZone? = nil) -> Any {
print ("copying game obj!")
let copy = Game()
copy.setGameModel(self)
copy.players = self.players // if I do not include it, copy.players is nil
return copy
}
}
class Player : NSObject, NSCopying, GKGameModelPlayer {
override var description: String {
return ("name: \(name), cash: $\(cash), wallet: $\(wallet.balance)")
}
internal var playerId: Int = 0 {
didSet {
print ("Set playerId = \(self.playerId)")
}
}
var name: String
var cash : Int = 0
var wallet : Wallet = Wallet()
init(name: String) {
self.name = name
}
func copy(with zone: NSZone? = nil) -> Any {
print ("copying player!!") // this code is never reached
let copy = self
copy.wallet = self.wallet.copy(with: zone) as! Wallet
return copy
}
}
enum WalletError : Error {
case mustBePositive
case notEnoughFunds
}
fileprivate protocol WalletDelegate {
var balance : Int { get }
func credit(amount: Int) throws
func debit(amount: Int) throws
}
class Wallet : NSCopying, CustomStringConvertible, WalletDelegate {
public private(set) var balance: Int = 0
func credit(amount: Int = 0) throws {
try canCredit(amount: amount)
self.balance += amount
}
func debit(amount: Int = 0) throws {
try canDebit(amount: amount)
self.balance -= amount
}
func copy(with zone: NSZone? = nil) -> Any {
print ("copy wallet") // this code is never reached
let copy = Wallet()
return copy
}
}
extension Wallet {
private func canCredit(amount: Int) throws {
guard amount > 0 else {
throw WalletError.mustBePositive
}
}
private func canDebit(amount: Int) throws {
guard amount > 0 else {
throw WalletError.mustBePositive
}
guard self.balance >= amount else {
throw WalletError.notEnoughFunds
}
guard (self.balance - amount >= 0) else {
throw WalletError.notEnoughFunds
}
}
}
extension Wallet {
var description: String {
return ("Balance: $\(self.balance)")
}
}
let players : [GKGameModelPlayer] = [ Player.init(name: "Bob"), Player.init(name: "Alex"), Player.init(name: "John") ]
let game = Game()
game.players = players
func copyTheGame() {
let copiedGame = game.copy() as! Game
print ("BEFORE:")
print ("Original: \(String(describing: game))")
print ("Copied: \(String(describing: copiedGame))")
print ("----")
if let copiedPlayers = copiedGame.players {
for p in copiedPlayers as! [Player] {
do {
p.cash = 100 // try to manipulate a class variable.
try p.wallet.credit(amount: 100)
} catch let err {
print (err.localizedDescription)
break
}
}
}
print ("AFTER:")
print ("Original: \(String(describing: game))")
print ("Copied: \(String(describing: copiedGame))")
print ("----")
}
copyTheGame()
In my output, I get the following:
copying game obj!
BEFORE:
Original: game: Optional([name: Bob, cash: $0, wallet: $0, name: Alex, cash: $0, wallet: $0, name: John, cash: $0, wallet: $0])
Copied: game: Optional([name: Bob, cash: $0, wallet: $0, name: Alex, cash: $0, wallet: $0, name: John, cash: $0, wallet: $0])
----
AFTER:
Original: game: Optional([name: Bob, cash: $100, wallet: $100, name: Alex, cash: $100, wallet: $100, name: John, cash: $100, wallet: $100])
Copied: game: Optional([name: Bob, cash: $100, wallet: $100, name: Alex, cash: $100, wallet: $100, name: John, cash: $100, wallet: $100])
----
Issues:
How do I ensure that any changes I make are only to the copied game object, and not the originals?
With thanks
Update:
I've been able to force the copy by looping through all the player objects, but I'm not convinced this is the best way to do it.
If I change the copy function in the game class to:
func copy(with zone: NSZone? = nil) -> Any {
let copy = Game()
let duplicate = self.players?.map({ (player: GKGameModelPlayer) -> GKGameModelPlayer in
let person = player as! Player
let copiedPlayer = person.copy(with: zone) as! Player
return copiedPlayer
})
copy.players = duplicate
return copy
}
This lets me make a duplicate of all the player objects, also; the copy() function in the player and wallet class get hit.
I've been able to resolve the issue by force copying reference objects, arrays or child objects
ie:
func copy(with zone: NSZone? = nil) -> Any {
let copy = Game()
let duplicate = self.players?.map({ (player: GKGameModelPlayer) -> GKGameModelPlayer in
let person = player as! Player
let copiedPlayer = person.copy(with: zone) as! Player
return copiedPlayer
})
copy.players = duplicate
return copy
}