TAGS :Viewed: 13 - Published at: a few seconds ago

[ application didFinishLaunchingWithOptions not getting called ]

I'm trying to figure why it doesn't seem "application didFinishLaunchingWithOptions" is being called when my app is starting up. My NSLog (@"Launch!") statement isn't showing up in the console, and the dictionary after it isn't being created either. If it makes a difference I'm using Storyboarding with a Navigation controller as the initial view. I tried adding a "object" to the Navigation controller and set it to "AppDelegate" and then setting it as the referencing outlet, but that didn't seem to make a difference either. Isn't this method supposed to be called every time the app launches? I even reset the simulator, but still nothing.

Thanks.

//
//  AppDelegate.m
//  PersonLibraryiOS
//
//  Created by Joey on 11/7/12.
//  Copyright (c) 2012 Joey. All rights reserved.
//

#import "AppDelegate.h"
#import "AddViewController.h"
#import "Person.h"
@implementation AppDelegate
@synthesize PersonDict;

-(void)addtoDict:(Person *)newPerson
{
    [PersonDict setObject:@"newPerson" forKey:[newPerson name]];
}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSLog (@"Launch!");

    // Override point for customization after application launch.
    return YES;
    PersonDict = [NSMutableDictionary dictionaryWithContentsOfFile:@"diskDict"];

Answer 1


EDIT: Must've been having a bad day when I wrote this originally. Removed the extremely unnecessary attitude from this post and clarified some things.

If you didn't know, return returns control to the caller of a method/subroutine. Since you return YES from the method before you create the dictionary, it never gets created and set to the property.

Additionally, the syntax for whatever you're trying to assign that NSMutableDictionary to is incorrect. It seems personDict is a property based on the @synthesize so:

You're accessing the instance variable backing it. You're supposed to access properties using dot notation like this:

self.personDict = [NSMutableDictionary dictionaryWithContentsOfFile:@"diskDict"];

//in addToDict
[self.personDict setObject:@"newPerson" forKey:[newPerson name]];

or using the setters and getters (dot notation has the identical effect):

[self personDict];
[self setPersonDict];

I don't think your usage of setObject:forKey: is what you intended.

//the object associated with the key will always be
//the NSString "newPerson" 
[self.personDict setObject:@"newPerson" forKey:[newPerson name]];
//did you mean to set the object to the method argument newPerson?
[self.personDict setObject: newPerson forKey:[newPerson name]];

Also, in Objective-C, you should name properties and variables in camel case, with the first letter being lower case. Like myCoolVar, or in this case, personDict rather than PersonDict; first letter capital camel case is used for class names, like your Person class.

Finally, if you manually @synthesize, it's a good idea to name the backing variable _<variable name> to avoid accessing it by accident.

@synthesize personDict = _personDict;