0

I'm trying to display some data on the flip side view of a utility template application but the application aborts at the end of viewDidLoad method. I'm very new to iOS and could do with a bit of guidance.

[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"SavedData"ofType:@"plist"];
NSMutableDictionary *tempRootDictionary;
NSMutableArray *tempMutableArray;
if (thePath && (tempRootDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:thePath])) {
    NSArray *keys = [tempRootDictionary allKeys];
    int keysCount = [keys count];
    tempMutableArray = [NSMutableArray arrayWithCapacity:keysCount];
    for (int i=0; i<keysCount; i++) {
        NSDictionary *dictionary = [tempRootDictionary objectForKey:[keys objectAtIndex:i]];
        MyModelObject *aModelObject = [[MyModelObject alloc] init];
        [aModelObject setName:[dictionary objectForKey:@"name"]];
        [aModelObject setContext:[dictionary objectForKey:@"context"]];
        [aModelObject setUsername:[dictionary objectForKey:@"username"]];
        [aModelObject setPassword:[dictionary objectForKey:@"password"]];
        [tempMutableArray addObject:aModelObject];
        [aModelObject release];
        [dictionary release];
    }
} else {
    return;
}

Help would be really appreciated, Many thanks...

Bilal Wahla
  • 665
  • 1
  • 12
  • 28
  • Anything suspicious in `MyModelObject`? – tia Nov 23 '10 at 03:10
  • 2
    Have you narrowed it down to the exact line that causes the error? At least the `[dictionary release];` line is wrong and should be removed. You didn't alloc `dictionary` so don't release it. –  Nov 23 '10 at 03:11

1 Answers1

1

The only obvious problem I see in the code posted is this:

[dictionary release];

On the line that you set dictionary, you are only getting a reference to the object in tempRootDictionary and not a new alloc'd instance of it. So don't release it. Remove that line.

  • Could you please also guide me to a resource where i can learn objective-c on the whole (whether i develop for macOS or iOS) and especially the basics like this one? – Bilal Wahla Nov 23 '10 at 04:37
  • See Apple's [Introduction To The Objective-C Programming Language](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html) and the [Memory Management Programming Guide](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html). –  Nov 23 '10 at 13:06
  • [This SO answer](http://stackoverflow.com/questions/2865185/do-you-need-to-release-parameters-of-methods-at-the-end-of-them-in-objective-c) by @Andiih also gives a nice acronym to remember the memory management rules by. –  Nov 23 '10 at 13:14