views:

59

answers:

3

Hi i have found a problem with my DataSource of my UITableView. Each time i try to fill the NSMutableArrayData in the method "addDataSection", the whole data-Array is set to the current Names-Array.

It all seems to work, until i write the Names into the Array and invoke [Names removeAllObjects]. Even in the method "addDataSection" my Names Array seems to be correct, so i maybe have a problem with a memory leak?

This ist the Implementation of the init method in the datasource-object (calls addDataSection):

-> watch the code below

has anyone ever seen problems like this occur in objective c?

+1  A: 

There are tons of leaks in your code. Here are some:

  • tempNames
  • currentPhoneNumber
  • currentEmail
  • tempDict

Also

[Names init];

Never ever send init to an object except straight after alloc.

JeremyP
A: 

You don't release tempDict.

And as says Ahmet, what is the [Names init] ?

Benoît
ok but thats not the problem at all, even if i release it i have those problems.
Name should be an argument for addDataSectionWithCategory. The problem is that Names is added to your dictionary, but after, you remove all objects. Names in not copied.
Benoît
Instead on [Names removeAllObjects]; you should make Names = [[NSMutableArray alloc] init];
Benoît
A: 

this ist not an answer but i have to write a piece of code that is better to read and understand...

My question is: why is my data array overwritten, and not the Names Array added one by one to my data Array?

-initWithDictionary:(NSDictionary*)theDictionary{
myDictionary = theDictionary;
data    = [[NSMutableArray alloc] init];
Names = [[NSMutableArray alloc] init];
NSDictionary* tempDict          = [[NSDictionary alloc] init];
NSString *currentFamilyName     = [[NSString alloc] init];
NSString *currentName           = [[NSString alloc] init];
NSString *currentPhoneNumber    = [[NSString alloc] init];
NSString *currentEmail          = [[NSString alloc] init];
NSString* currentCategory = @"a"; //set the First Category (A - Z)

for (NSDictionary *anyID in [[[myDictionary objectForKey:@"ifeed"]objectForKey:@"channel"] objectForKey:@"item"]) {//For Any Item in XML Dictionary
    //FamilyName and Name do always exist
    currentFamilyName       = [NSString stringWithString:[[[anyID objectForKey:@"n"]objectForKey:@"family-name"] objectForKey:@"content"]];
    currentName             = [NSString stringWithString:[[[anyID objectForKey:@"n"]objectForKey:@"given-name"] objectForKey:@"content"]];
    currentPhoneNumber      = [[NSString alloc] initWithString:@""]; //we dont want the last phoneNumber
    currentEmail            = [[NSString alloc] initWithString:@"keine Email hinterlegt"]; //we dont want the last email address


    for (NSDictionary *currentDict in [anyID objectForKey:@"tel"]){ //Current XML Item in my Dictionary -> telephone number Element "tel"
        if ([[[currentDict objectForKey:@"attributes"] objectForKey:@"type"] isEqualToString:@"work"]) {//if tel has the attribute "work"
            currentPhoneNumber = [currentDict objectForKey:@"content"]; //set items phone Number
        }
    }

    if ([[anyID objectForKey:@"email"] objectForKey:@"content"] != nil){
        currentEmail = [currentEmail stringByAppendingString:[[anyID objectForKey:@"email"] objectForKey:@"content"]];
    }


    if ([[[currentFamilyName substringToIndex:1] lowercaseString] isEqualToString:[currentCategory lowercaseString]] == NO && Names != nil){
    //if the first Letter of the Name is not equal to the Category (A - Z)
        NSLog(@"adding the category-section:%@ with %d items to data", currentCategory, [Names count]);
        [self addDataSectionWithCategory:currentCategory]; //add a new Names Section to the data Array
        currentCategory = [currentFamilyName substringToIndex:1]; //set last Category 
        NSLog(@"changed Category to: %@", currentCategory);
    }

    tempDict =  [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: currentFamilyName, currentName,                                                  currentPhoneNumber, currentEmail, nil] 
                                            forKeys:[NSArray arrayWithObjects: @"name", @"vorname", @"nummer", @"email", nil]];
    [Names addObject:tempDict];
            [tempDict release];

}

return self;    

}

-(void)addDataSectionWithCategory:(NSString*)cat_{ //add one Section to data Array - Sections should be displayed in the UITableView
//if you log Names here it all will be good. Names will have the correct items.
[data addObject: [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: cat_, self.Names, nil] 
                                             forKeys:[NSArray arrayWithObjects: @"header", @"data", nil]]]; //data seems to be overwritten

[Names removeAllObjects]; //clear Name for the next section (A - Z) of "Names"

}

You should add this on your question !
Benoît
ok i found an Answer believe it or not i have to use [Names copy] in addDataSection.