0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

NSArrayにnilを入れても落ちない理由

Posted at
NSString *nilString;
NSArray *array = @[@"hogehoge", nilString, @100];

なんてことをすると、

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[1]'

と落ちちゃうので、落ちない == nilは来てないと思い込んでいたら、

NSArray *array = [NSArray arrayWithObjects:@"hogehoge", nilString, @100, nil];
NSLog(@"%@", array);
//=>(
//=>    hogehoge
//=>)

こんな懐かしい書き方をしているものだから、nilStringが終端になっていて落ちない & nilString以降が切り捨てられている、という状況でした。

おしまい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?