Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding and writing singleton class instead of using delegate class
    text
    copied!<p>I'm new in iOS programming and I am very interested in what singleton class is, and why it is used. I found some information but it is vague. In particular I would like to apply it to real example. My project use Facebook SDK and I want to create singleton class for my NSDictionary which contain friends list. My .m delegate file:</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //here is some other code facebook = [[Facebook alloc] initWithAppId:@"my app id" andDelegate:self]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if ([defaults objectForKey:@"FBAccessTokenKey"] &amp;&amp; [defaults objectForKey:@"FBExpirationDateKey"]) { facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; } if (![facebook isSessionValid]) { NSArray *permissions = [[NSArray alloc] initWithObjects: @"user_location", @"friends_location", @"read_friendlists", nil]; [facebook authorize:permissions]; [permissions release]; } [facebook requestWithGraphPath:@"me/friends" andDelegate:(id)self]; //here is some other code } </code></pre> <p>And I set to my NSDictionary value of request which return friends list:</p> <pre><code>- (void)request:(FBRequest *)request didLoad:(id)result { _friendsDictionary = result; } </code></pre> <p>In which way I need to write singleton class and do not use delegate class <code>AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload