ios - How to create 2-dimensional array with nil elements -
in chess game use 2-dimensional array track positions of pieces @ chess board.
initially thought create nsmuteablearray
, indicate unoccupied squares nil
. occupied slots should hold pointer piece
object...
however following code:
nsmutablearray* _board; ... _board = [[nsmutablearray alloc] init]; (int = 0; < 8; i++) { nsmutablearray *row = [[nsmutablearray alloc] init]; (int j = 0; j < 8; j++) { [row addobject:nil]; } [_board addobject:row]; }
fails runtime error:
*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '*** -[__nsarraym insertobject:atindex:]: object cannot nil'
so nil
can't passed argument addobject
... here instead?
try
nsmutablearray* _board; ... _board = [[nsmutablearray alloc] init]; (int = 0; < 8; i++) { nsmutablearray *row = [[nsmutablearray alloc] init]; (int j = 0; j < 8; j++) { [row addobject:[nsnull null]]; } [_board addobject:row]; }
reason using [nsnull null]
instead of nil
can seen here
Comments
Post a Comment