title: Retain count and factory methods in Objective-C
body: <p>I have been in this forum before to find the best way of creating a factory function to construct a custom view from a nib (<a href="https://stackoverflow.com/questions/7191959/question-about-init-and-load-from-xib-for-a-custom-uiview">here</a> is my previous post)</p>
  
  <p>i am now using the following code:</p>
  
  <pre><code>+ (LoadingV *)loadingViewCopyFromNib 
  {   
      LoadingV *view = nil;   
      NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"LoadingV" owner:self options:nil];
      view = (LoadingV*)[nibViews objectAtIndex: 0];
  
      // Setting up properties
      CGRect frm = view.progress.frame;
      frm.size.width *=1.5;
      frm.size.height *=1.5;
      view.progress.frame = frm;
      view.waitLbl.text = NSLocalizedString(@"Please wait", @"");     
      return view;    &lt;------- warning is here
  }
  
  // In .h file
  ...
  LoadingView* loadV;
  @property (nonatomic, retain) LoadingView* loadV;
  
  // in .m file
  @synthesize loadV;
  ...
  self.loadV = [LoadingV loadingViewCopyFromNib];
  </code></pre>
  
  <p>When i Build and Analyse i get the following warning about the factory function:</p>
  
  <blockquote>
    <p>/LPAPP/Classes/LoadingV.m:34:5 Object with +0 retain counts returned
    to caller where a +1 (owning) retain count is expected</p>
  </blockquote>
  
  <p>Why is this happening? I understand that local variables allocated within a function do not live beyond its scope unless they are retained and autoreleased. But in my case i am not creating a new object am just returning a reference to an existing one. So why am i getting warning back? Is it safe to proceed like this :)</p>
  
  <p>Cheers
  AF</p>