NSCopy GKGameModel 不能正确复制玩家对象

NSCopy GKGameModel doesn't copy player objects correctly

我正在尝试使用 swift NSCopy 对 GKGameModel 的对象进行深度复制,包括所有玩家及其钱包参考(包含代表他们现金的整数)。

使用 Swift playgrounds 我尝试将 100 美元记入所有复制的玩家,但保持原始玩家对象完好无损。

但是,我注意到代码也会影响原始播放器对象。

在高层次上,代码应显示:

Game class, a GKGameModel
Player class, a GKPlayerModel
Wallet class, handles very basic player's Int transactions.

目标:

代码如下:

// 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()

在我的输出中,我得到以下内容:

copying game obj!
BEFORE:
Original: game: Optional([name: Bob, cash: [=12=], wallet: [=12=], name: Alex, cash: [=12=], wallet: [=12=], name: John, cash: [=12=], wallet: [=12=]])
Copied: game: Optional([name: Bob, cash: [=12=], wallet: [=12=], name: Alex, cash: [=12=], wallet: [=12=], name: John, cash: [=12=], wallet: [=12=]])
----
AFTER:
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])
----

问题:

如何确保我所做的任何更改仅针对复制的游戏对象,而不是原始游戏对象?

谢谢


更新:

我已经能够通过遍历所有玩家对象来强制复制,但我不认为这是最好的方法。

如果我把游戏里的复制功能class改成:

 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
    }

这也让我复制了所有玩家对象;播放器和钱包中的 copy() 函数 class 受到攻击。

我已经能够通过强制复制引用对象、数组或子对象来解决问题

即:

 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
    }