title: Accessing ivar of an instance of the same class in a class method
body: <p>Edit 2: In addition to Kurt's solution, there is one more way to do it. Take a look at the end of this page, just before comments: <a href="http://www.friday.com/bbum/2009/09/11/class-extensions-explained/" rel="nofollow">http://www.friday.com/bbum/2009/09/11/class-extensions-explained/</a></p>
  
  <hr>
  
  <p>Edit: It seems class methods in a class category cannot access private members such as ivars and private methods that are implemented through class extensions.</p>
  
  <hr>
  
  <p>I hope this question is not asked and answered before, but I could not find one as both stackoverflow and Google search spams my browser window with kinds of questions that ask to access an ivar directly from a class method, which is clearly not my intention.</p>
  
  <p>Straight to the problem, I'll provide a piece of code, which summarizes what I'm trying to accomplish:</p>
  
  <hr>
  
  <p>XYZPerson.h:</p>
  
  <pre><code>@interface XYZPerson : NSObject
  @property (weak, readonly) XYZPerson *spouse;
  @end
  </code></pre>
  
  <p>XYZPersonMariage.h:</p>
  
  <pre><code>@interface XYZPerson (XYZPersonMariage)
  +(BOOL)divorce:(XYZPerson *) oneOfSpouses;
  @end
  </code></pre>
  
  <p>XYZPersonMariage.m</p>
  
  <pre><code>+(BOOL)divorce:(XYZPerson *)oneOfSpouses
  {
    XYZPerson *otherSpouse = [oneOfSpouses spouse];
    if(otherSpouse != nil)
    {
      oneOfSpouses-&gt;_spouse = nil;
      otherSpouse-&gt;_spouse = nil;
      return true;
    }
    return false;
  }
  </code></pre>
  
  <p>I first thought that maybe an ivar is not automatically synthesized for a property flagged readonly, but it is indeed synthesized.</p>
  
  <p>So, what paths can I take to get the job done?</p>