0

I am using MultipeerConnectivity. I need to convert my [Move?] array to data when sharing between two devices.

first i create my moves variable.

@Published var moves: [Move?] = Array(repeating: nil, count: 9)

here I am trying to convert [Moves?] Array to Data but I am getting error here. Why ?

NSKeyedArchiver.archivedData throws an error.

Error:

Thread 1: signal SIGABRT

NSForwarding: warning: object 0x6000029c64f0 of class 'MultipleTicTacToe.Move' does not implement methodSignatureForSelector: -- trouble ahead

Array to Data Function:

func sendMoves(moves: [Move?]) {
    if session.connectedPeers.count > 0 {
        do {
            let encodedData = try NSKeyedArchiver.archivedData(withRootObject: moves, requiringSecureCoding: true)
            try self.session.send(encodedData, toPeers: session.connectedPeers, with: .unreliable)
        } catch let error {
            print(error.localizedDescription)
        }
    }
}

My Model:

enum Player {
    case player1
    case player2
    case ai
    case none
}

class Move {
    
    let player: Player
    let boardIndex: Int
    var indicator: UIBezierPath
    
    init(player: Player, boardIndex: Int, indicator: UIBezierPath) {
        self.player = player
        self.boardIndex = boardIndex
        self.indicator = indicator
    }
}
Ufuk Köşker
  • 1,288
  • 8
  • 29
  • If you use `NSKeyedArchiver` on an array of `Move`, `Move` needs to be `NS(Secure)Coding` Compliant which isn't the case. Also, print error, not error.localizedDescription (which is for User, not Developer) – Larme Sep 03 '21 at 09:09
  • What's the point having optionals in your Array? This may be only a temporar result, before you apply compactMap. For any transport between components, it would make more sense to have `[Move]`. Also, you may consider to use JSON as a data transform format. – CouchDeveloper Sep 03 '21 at 11:18
  • For the TicTacToe game, moves and indexes must be empty at first. – Ufuk Köşker Sep 03 '21 at 11:23
  • How can I encode UIBezierPath to json? I couldn't find it. – Ufuk Köşker Sep 03 '21 at 11:24
  • Why do you keep a UIBezierPoint if it's a TicTacToe Game? Shouldn't you keep coordinates only? – Larme Sep 03 '21 at 12:41

0 Answers0