title: Advice on how to catch "attempt to insert nil object" from a device needed
body: <p>Here is a situation:
  Hockeyapp and testflight every now and then complain about me </p>
  
  <blockquote>
    <p>"attempting to insert nil object"</p>
  </blockquote>
  
  <p>in mutable dictionaries/arrays. I know the right thing is to check for nil all the time, and I do when it makes sense.. Our testers can not catch those crashes, but AppStore users obviously can.</p>
  
  <p>My guess is that sometimes server returns NSNulls when it should not. 
  So not to insert checks for nil everywhere in the huge project my idea was to create a separate target for the testers and use method swizzling for collection classes. 
  Say, I'll replace <code>insertObject:atIndex</code> with my <code>swizzled_insertObject:atIndex</code>, where if the object is actually nil I log/show a descriptive report before it crashes. </p>
  
  <p>The thing is I can not use swizzling for <code>__NSPlaceholderDictionary</code> or <code>__NSArrayM</code>  (just because I can not make a category on private classes) and that makes me sad. </p>
  
  <p>So basically I'm asking for advice on how to catch those nasty rare crashes. 
  One solution I have in mind is using try-catch blocks, I know they are expensive in Objective-c, so I'd not use them in production, just for testers. But methods surrounded by <code>try-catche</code>-s surrounded by <code>#ifdef</code>-<code>#endif</code>-s will erase all the readableness of the code. So I'm searching for a more elegant solution. 
  Thanks. </p>
  
  <p>Update: the stack traces are unfortunaely not very descriptive, here is what I get</p>
  
  <pre><code>Exception Type:  SIGABRT
  Exception Codes: #0 at 0x3a378350
  Crashed Thread:  0
  
  Application Specific Information:
  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[2]'
  
  Last Exception Backtrace:
  0   CoreFoundation                      0x321522a3 &lt;redacted&gt; + 163
  1   libobjc.A.dylib                     0x39e7a97f _objc_exception_throw + 31
  2   CoreFoundation                      0x320a355f &lt;redacted&gt; + 135
  3   CoreFoundation                      0x320da0d3 &lt;redacted&gt; + 51
  ....
  </code></pre>