2016-12-06 9 views
1

私はインターウェブ全体、特にスタックオーバーフローと私が自宅で持っている参考文献を研究していますが、私のコードで何が間違っているのか理解できませんでした。私はこの問題を理解しようと多くの時間を費やしてきました。ここで誰かが正しい方向に向けるのを助けてくれることを願っています。カスタムオブジェクトをNSData Swiftに変換する

セットアップ:
私はスウィフト3.0で構築していチェスのアプリを持っているし、次のような構造がある:BoardModelは、ピースがボード内のクラスのゲームに関するデータのすべてを保持しているクラスですモデルには、BoardModel内のデータを保持するモデル、.knight、.kingなどのPieceType列挙もあります。

BoardModelには、チェスボードを表すPieceの2D配列があります。今度は、ゲームセンターにゲームデータを保存したいのですが、ゲームデータを保存しようとする前に、エンコーディングでエラーが発生し、それが私の居場所です。このエラーは、AppDelegateのステートメントが"Thread: 1 signal SIGABRT"であることを示しています。ここで

はワンピースのクラスと一緒に問題のあるコードです:

let pieceData = NSKeyedArchiver.archivedData(withRootObject: board.board[0][0]) // where the error is thrown 

class Piece: NSObject, NSCoding { 
    var isSelected: Bool 
    var type: PieceType 
    var isWhite: Bool 
    var isFirstMove: Bool 
    var symbol: String! 
    var position: (row: Int, col: Int)! 

    override init() { 
     isSelected = false 
     type = PieceType.empty 
     isWhite = true 
     isFirstMove = true 
    } 

    required init(coder aDecoder: NSCoder) { 
     isSelected = aDecoder.decodeBool(forKey: "isSelected") 
     type = aDecoder.decodeObject(forKey: "type") as! BoardModel.PieceType 
     isWhite = aDecoder.decodeBool(forKey: "isWhite") 
     isFirstMove = aDecoder.decodeBool(forKey: "isFirstMove") 
     symbol = aDecoder.decodeObject(forKey: "symbol") as! String 
     position = aDecoder.decodeObject(forKey: "position") as! (Int, Int) 
    } 

    func encode(with aCoder: NSCoder) { 
     aCoder.encode(isSelected, forKey: "isSelected") 
     aCoder.encode(type, forKey: "type") 
     aCoder.encode(isWhite, forKey: "isWhite") 
     aCoder.encode(isFirstMove, forKey: "isFirstMove") 
     aCoder.encode(symbol, forKey: "symbol") 
     aCoder.encode(position, forKey: "position") 
    } 

    init(isSelected: Bool, type: PieceType, isWhite: Bool, isFirstMove: Bool, symbol: String, position: (Int, Int)) { 
     self.isSelected = isSelected 
     self.type = type 
     self.isWhite = isWhite 
     self.isFirstMove = isFirstMove 
     self.symbol = symbol 
     self.position = position 
    } 

    func setPosition(to newPosition: (row: Int, col: Int)) { 
     position = newPosition 
    } 
} 
+0

あるPieceTypeは何ですか? Enumまたはカスタムオブジェクト(NSObject)ですか? –

+0

@jigneshVadadoriya PieceTypeが列挙型です –

+0

位置データ型にNSCoderを提供しましたか?あなたのコードから位置をコメントアウトして、まだクラッシュしていないかどうかを確認してください。 PieceTypeのNSCoderも、実装した方法に応じて提供する必要があります。 – jvarela

答えて

1

あなたEnumこのPieceTypeのようなもので、タイプはInt

enum PieceType : Int { 
    case empty 
    case notEmpty 

} 

その後のようなencodedecode方法を記述している場合この方法で

required init(coder aDecoder: NSCoder) { 
     isSelected = aDecoder.decodeBool(forKey: "isSelected") 
     type = PieceType(rawValue: aDecoder.decodeObject(forKey: "type") as! Int)! 
     isWhite = aDecoder.decodeBool(forKey: "isWhite") 
     isFirstMove = aDecoder.decodeBool(forKey: "isFirstMove") 
     symbol = aDecoder.decodeObject(forKey: "symbol") as! String 
     position = aDecoder.decodeObject(forKey: "position") as! (Int, Int) 
    } 

    func encode(with aCoder: NSCoder) { 
     aCoder.encode(isSelected, forKey: "isSelected") 
     aCoder.encode(type.rawValue, forKey: "type") 
     aCoder.encode(isWhite, forKey: "isWhite") 
     aCoder.encode(isFirstMove, forKey: "isFirstMove") 
     aCoder.encode(symbol, forKey: "symbol") 
     aCoder.encode(position.row, forKey: "position.row") 
     aCoder.encode(position.col, forKey: "position.col") 
    } 

私は怒鳴るコードをチェックして、それが仕事の

let pice = Piece(isSelected: true, type: .empty, isWhite: true, isFirstMove: true, symbol: "Test", position: (2, 10)) 
let pieceData = NSKeyedArchiver.archivedData(withRootObject:pice) 
print(pieceData) 

アウトプットが

386 bytes 
+0

これはあなたの例でインスタンシエートしたような空白の部分だけで動作します。 ----------------------- let somePiece = BoardModel.Piece(isSelected:false、type:BoardModel.PieceType.knight、 isWhite:true、isFirstMove:true、symbol: "Wknight"、位置:(0、1)) –

+0

ポジションインスタンスを削除しても機能しましたが、ポジションが必要です。 @jvarela、私の質問にコメントし、位置のNSCoderを提供すると述べたが、私はこれを行う方法が不明です。 jignesh、これを行うにはどのように最善の考えがありますか? –

+0

他の方法で試してみましょう –

関連する問題