title: iphone - testing if an object exists
body: <p>I have several objects in my app that can become nil at some point and I have methods that in theory are used to put these objects to nil.</p>
  
  <p>But, if I try to put to nil an object that does not exist, the app will crash.</p>
  
  <p>for example...</p>
  
  <pre><code>[object1 release];
  object1 = nil;
  </code></pre>
  
  <p>//... and after that</p>
  
  <pre><code>[object1 removeFromSuperview]; // this will crash
  </code></pre>
  
  <p>Then I thought, why not testing to see if the object exists before removing...</p>
  
  <pre><code>if (object1 != nil)
   [object1 removeFromSuperview]; 
  // this will crash too, because object1 cannot be tested for nil because it does not exist
  </code></pre>
  
  <p>How can I check if the object exists before testing if it is nil?
  something as</p>
  
  <pre><code>if (object1 exists( {
    if(object1 != nil))
      [object1 removeFromSuperview)
  }
  </code></pre>
  
  <p>is this possible?</p>
  
  <p>I ADDED THIS TO CLARIFY...</p>
  
  <p>what I mean is this: imagine I have object1 declared on the header and initialized on the code. So, it exists and points to a valid reference. At some point, the object may be released, so the reference still points to an object but the object was freed. Even if I put the object to nil after releasing, I cannot do anything with it.</p>
  
  <p>The problem is this: I have some methods that are asynchronous. One of them scans for some objects and remove them if they are found. I must check if the object exists and the reference points to a valid object before releasing it again. This is the point: how do I test if the object exists and its reference points to a valid existent object before releasing it again, to void releasing again an object that is already released and crash the app.</p>