title: Custom delegate not working
body: <p>I have started working more with delegate as suggested in another question I made. Now, I have  made a UIViewController called <code>ProfileViewController</code> in which I would like to load the News Feed from Facebook. I also have subclass of NSObject called <code>FBFeed</code>. This subclass loads the Facebook News Feed and should pass it back to the view controller. All my requests to Facebook are sent through a singleton named <code>FBRequestWrapper</code> which (in this case) should pass the result to <code>FBFeed</code>.</p>
  
  <p>I call <code>getFBRequestWithGraphPath:andDelegate:</code> in <code>FBRequestWrapper</code> which will call a method in the Facebook iOS SDK which takes a delegate as a parameter. If I put in <code>self</code> (which will be the FBRequestWrapper) it works just fine. If I put in <code>_delegate</code> (which is an instance of <code>FBFeed</code>) the application crashes with <code>EXC_BAD_ACCESS</code>.</p>
  
  <p>I have a theory why it might crash. I have read that</p>
  
  <pre><code>@property (nonatomic, retain) id &lt;FBFeedDelegate&gt; delegate;
  </code></pre>
  
  <p>should be</p>
  
  <pre><code>@property (nonatomic, assign) id &lt;FBFeedDelegate&gt; delegate;
  </code></pre>
  
  <p>But when doing so I get the following error:</p>
  
  <blockquote>
    <p>Existing ivar 'delegate' for unsafe_unretained property 'delegate'
    must be __unsafe_unretained</p>
  </blockquote>
  
  <p>Also, I don't know if that enough to make the application crash.</p>
  
  <p>Here is my code.</p>
  
  <p><strong>ProfileViewController.h</strong></p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "FeedTableView.h"
  #import "FBFeed.h"
  
  @interface ProfileViewController : UIViewController &lt;FBFeedDelegate&gt;
  {
      FeedTableView *tableView;
  }
  
  - (void)loadFeed;
  
  @end
  </code></pre>
  
  <p><strong>ProfileViewController.m</strong></p>
  
  <pre><code>@implementation ProfileViewController
  
  - (void)loadFeed
  {
      FBFeed *feed = [[FBFeed alloc] init];
      [feed loadNewsFeedWithDelegate:self];
  }
  
  #pragma mark -
  #pragma FBFeedDelegate
  
  - (void)finishedLoadingFeed:(FBFeed *)_feed
  {
      NSLog(@"ProfileViewController: FBFeed Finished.");
  }
  
  - (void)failedToLoadFeed:(FBFeed *)_feed
  {
      NSLog(@"ProfileViewController: FBFeed Failed.");
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      // Load feed
      [self loadFeed];
  }
  
  @end
  </code></pre>
  
  <p><strong>FBFeed.h</strong></p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "FBRequestWrapper.h"
  
  @protocol FBFeedDelegate;
  
  @interface FBFeed : NSObject &lt;FBRequestDelegate&gt;
  {
      id &lt;FBFeedDelegate&gt; delegate;
  }
  
  @property (nonatomic, retain) id &lt;FBFeedDelegate&gt; delegate;
  
  - (void)loadNewsFeedWithDelegate:(id)_delegate;
  
  @end
  
  
  @protocol FBFeedDelegate &lt;NSObject&gt;
  @required
  - (void)finishedLoadingFeed:(FBFeed *)_feed;
  - (void)failedToLoadFeed:(FBFeed *)_feed;
  @end
  </code></pre>
  
  <p><strong>FBFeed.m</strong></p>
  
  <pre><code>#import "FBFeed.h"
  
  @implementation FBFeed
  
  @synthesize delegate;
  
  - (void)loadNewsFeedWithDelegate:(id)_delegate
  {
      self.delegate = _delegate;
  
      [[FBRequestWrapper defaultManager] getFBRequestWithGraphPath:@"me" andDelegate:self];
  }
  
  #pragma mark -
  #pragma mark FBRequest Delegate
  
  - (void)request:(FBRequest *)request didLoad:(id)result
  {
      NSLog(@"FBFeed: FBRequest Did Load.");
      NSLog(@"%@", result);
  }
  
  - (void)request:(FBRequest *)request didFailWithError:(NSError *)error
  {
      NSLog(@"FBFeed: FBRequest Failed.");
      NSLog(@"%@", error);
  }
  
  @end
  </code></pre>
  
  <p><strong>FBRequestWrapper.h</strong></p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "FBConnect.h"
  
  @interface FBRequestWrapper : NSObject &lt;FBRequestDelegate, FBSessionDelegate&gt;
  {
      Facebook *facebook;
      BOOL isLoggedIn;
  }
  
  @property (nonatomic, assign) BOOL isLoggedIn;
  
  + (id)defaultManager;
  - (void)setIsLoggedIn:(BOOL)_loggedIn;
  - (void)FBSessionBegin:(id&lt;FBSessionDelegate&gt;)_delegate;
  - (void)FBLogout;
  - (void)getFBRequestWithGraphPath:(NSString *)_path andDelegate:(id)_delegate;
  - (void)getFBRequestWithMethodName:(NSString *)_methodName andParams:(NSMutableDictionary *)_params andDelegate:(id)_delegate;
  
  @end
  </code></pre>
  
  <p><strong>FBRequestWrapper.m</strong></p>
  
  <pre><code>#import "FBRequestWrapper.h"
  
  static FBRequestWrapper *defaultWrapper = nil;
  
  @implementation FBRequestWrapper
  @synthesize isLoggedIn;
  
  + (id)defaultManager
  {
      if(!defaultWrapper)
      {
          defaultWrapper = [[FBRequestWrapper alloc] init];
      }
  
      return defaultWrapper;
  }
  
  - (void)getFBRequestWithGraphPath:(NSString *)_path andDelegate:(id)_delegate
  {
      if (_path != nil)
      {
          [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  
          if (_delegate == nil)
          {
              _delegate = self;
          }
  
          [facebook requestWithGraphPath:_path andDelegate:_delegate];
      }
  }
  
  #pragma mark -
  #pragma mark FBRequestDelegate
  
  - (void)request:(FBRequest *)request didLoad:(id)result
  {
      [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
      NSLog(@"%@", result);
  }
  
  - (void)request:(FBRequest *)request didFailWithError:(NSError *)error
  {
      [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
      NSLog(@"FBRequest Failed: %@", error);
  }
  
  @end
  </code></pre>