Where do I initialise NSDictionary

Discussion in 'iOS Programming' started by GerardGrundy, Sep 8, 2013.

  1. GerardGrundy, Sep 8, 2013
    Last edited: Sep 8, 2013

    GerardGrundy macrumors newbie

    Joined:
    Sep 8, 2013
    #1
    This is what I have though every time I run the method it initialises the Dictionary though I want to add more information to it for the duration of the program.

    This is for an application for iPhone.
    In the header file I've got:



    Code:
    @interface ViewController : UIViewController 
    
    { 
    
        NSMutableDictionary *dictionary; 
    
      } 
    
    -(void)viewDidLoad; 
    
    @property (nonatomic, retain) NSDictionary *dictionary;



    In the .m file I have this as I'm trying to initialize it here as I read to take this out of the function to stop it from resetting the Dictionary. Though I cant get this to work.


    Code:
    -(void)viewDidLoad 
    
    { 
    
        self.dictionary = [[NSMutableDictionary alloc] init ]; 
    
    } 

    in the method where I'm adding to the Dictionary I can only get things adding to it when I put:


    Code:
    dictionary = [[NSMutableDictionary alloc] init ]; 


    Where should I put the line:


    Code:
    dictionary = [[NSMutableDictionary alloc] init ]; 

    as it is doing nothing in viewDidLoad and resets the dictionary in the method where I'm setting objects.
    Code:
    //this is after the viewDidLoad Method
    -(void)addToNSDict: (NSString*)value
    {
      //I think this is the one that is resetting the dictionary 
      //so I'm unsure where to put it
        dictionary = [[NSMutableDictionary alloc] init ];
       [ dictionary setObject:value forKey:@"Current Number"];
    // prints the current dictionary
       CFShow (CFBridgingRetain(dictionary));
    }
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Post the code of the method where you're adding objects. Post complete code, not fragments.

    Then describe exactly when the method adding objects is being called. Is it before or after the viewDidLoad method? Post that code, too.
     
  3. pmau macrumors 65816

    Joined:
    Nov 9, 2010
    #3
    Instance variables declared in the interface could arguably be initialized in the "init" method of your view controller.

    But I'm posting for another reason.

    UIViewControllers are usually short lived, for example if they are pushed on a navigation controller stack for a short time.

    One additional idea might be to wire your model in the AppDelegate class of your app and reference the data from your UIViewController.

    (If you really intend to keep the data as long as your app is running).

    When handling model data it is always important to consider the lifetime of the data, or to put it differently:

    How long do you really need the data to be around?

    And: How hard would it be to recreate it?
     
  4. GerardGrundy thread starter macrumors newbie

    Joined:
    Sep 8, 2013
    #4
    Is that all the code you need?

    Hi I hope that's all the code you need. Thanks for your help.
     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    alloc/initing a dictionary in viewDidLoad is reasonable if you fill the dictionary during the lifetime of the view controller and then discard it when you're done.

    If you need the dictionary to persist longer than the view controller, you should create it and save it somewhere else. (The other poster recommended adding a property to the app delegate. That would work, but I prefer adding a data container singleton to my apps to save app-wide variables. That's more complexity than I want to get into here however.)

    You should NOT alloc/init a new dictionary in your addToNSDict method. That is wrong on several levels.


     
  6. GerardGrundy, Sep 8, 2013
    Last edited: Sep 8, 2013

    GerardGrundy thread starter macrumors newbie

    Joined:
    Sep 8, 2013
    #6
    -(id) init

    Hi I've added

    Code:
    -(id)init
    {
       
        dictionary = [[NSMutableDictionary alloc] init ];
    }
    I've added this code to app delegate though it has an error:
    Control reaches end of non-void function. Would you know why it would say this?

    Code:
    -(id)init
    {
       
        dictionary = [[NSMutableDictionary alloc] init ];
        return self;
    }

    Ok I've added this to the app delgate though I'm the NsDictionary is still resetting.
     
  7. pmau macrumors 65816

    Joined:
    Nov 9, 2010
    #7
    A proper init function:

    Code:
    - (id)init
    {
        self = [super initWithNibName:nil bundle:nil];
    
        if (self) {
           self.dictionary = [[NSMutableDictionary alloc] init];
        }
    
        return self;
    }
    
    You always have to call the base class constructor using super.
    And ofcourse return the initialized instance.

    I used nil for the arguments as you can see.
    Read the docs for designated initializers of classes.

    Most of them have one.
    Cheers
     
  8. GerardGrundy thread starter macrumors newbie

    Joined:
    Sep 8, 2013
    #8
    No visible @ interface


    I tried this though got this error:
    No visible @interface for 'UIResponder' declares the selector 'initWithNibName:bundle:'
     
  9. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #9

    No, init for his app delegate should not call initWithNibName. Even for a view controller, you shouldn't call super initWithNibName in init. You would only call super initWithNibName in an override of initWithNibName.


    It should be something like this:

    Code:
    - (id) init;
    {
      self = [super init];
      if (self)
      {
         self.dictionary = [[NSMutableDictionary alloc] init];
      }
      return self;
    }
    To the OP, if you don't know how to override an init method, you need to do some reading on the basics of the language.
     
  10. iMacFarlane macrumors 65816

    iMacFarlane

    Joined:
    Apr 5, 2012
    Location:
    Adrift in a sea of possibilities
    #10
    Another problem I see is that you declare your instance variables as type NSMutableDictionary, but then you declare your @property as type NSDictionary. Can't think of anything good coming of that.

    Typically, if you are declaring an iVar as a @property, you don't need to declare it redundantly as an iVar (in the {braces}).

    And, as far as where to initialize your dictionary? If you really only need the data in the dictionary to be around when the viewcontroller's view is onscreen, it doesn't matter if you fill it in viewWillLoad, viewDidLoad, or some form of init for the controller.

    Look, I'm just starting out on this whole thing, too. I strongly recommend Steven Kochan's "Objective-C Programming (5th Edition)", followed by the Big Nerd Ranch's "iOS Programming" books. I just spent the last few months working through them, and am just about ready to start seriously producing some quality apps. :)

    Cheers.
     

Share This Page