Model class for NSDictionary information with Lazy Loading

Posted by samfu_1 on Stack Overflow See other posts from Stack Overflow or by samfu_1
Published on 2010-04-06T16:24:42Z Indexed on 2010/04/06 17:13 UTC
Read the original article Hit count: 229

Filed under:
|
|

My application utilizes approx. 50+ .plists that are used as NSDictionaries.

Several of my view controllers need access to the properties of the dictionaries, so instead of writing duplicate code to retrieve the .plist, convert the values to a dictionary, etc, each time I need the info, I thought a model class to hold the data and supply information would be appropriate.

The application isn't very large, but it does handle a good deal of data. I'm not as skilled in writing model classes that conform to the MVC paradigm, and I'm looking for some strategies for this implementation that also supports lazy loading..

This model class should serve to supply data to any view controller that needs it and perform operations on the data (such as adding entries to dictionaries) when requested by the controller

functions currently planned:

  • returning the count on any dictionary
  • adding one or more dictionaries together

Currently, I have this method for supporting the count lookup for any dictionary. Would this be an example of lazy loading?

-(NSInteger)countForDictionary: (NSString *)nameOfDictionary {

    NSBundle *bundle  = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource: nameOfDictionary ofType: @"plist"];  

    //load plist into dictionary
    NSMutableDictionary *dictionary  = [[NSMutableDictionary alloc] initWithContentsOfFile: plistPath];
    NSInteger count = [dictionary count]

    [dictionary release];

    [return count]
}

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about objective-c