Skip navigation.
 
mlRe: Please help a newbie in distress
FROM : Andrew Zamler-Carhart
DATE : Sun Jan 26 21:39:01 2003

Rod,

The problem you are having is that each time your loop runs, it uses
the same NSMutableDictionary object. When you add the dictionary to the
array, it doesn't make a deep copy -- it just adds a reference to the
record. So you wind up adding several references to the same object,
which is why every row in your table always shows the same data. Each
time your loop iterates, you clobber the data in the record, which is
why the table objects always show the data for the *last* record that
you added.

What you need to do is create a new NSMutableDictionary object each
time your loop iterates. You should wind up with something like this:

NSMutableArray *records = [NSMutableArray array];
NSMutableDictionary *record;
for (i = 0; i < count; i++) {
   record = [NSMutableDictionary dictionary];
   // initialize the record with data
   [records addObject: record];
}

When this code is finished, the records array will contain <count>
unique dictionaries. I don't know how much Objective-C memory
management you've encountered so far, but in the code above everything
will be handled properly. Each time you call [NSMutableDictionary
dictionary] you get a dictionary that has been autoreleased. Arrays
retain all objects that are added to them. As long as you retain your
array, you'll be in good shape.

If that doesn't work, be sure that you've implemented
tableView:objectValueForTableColumn:row: in such a way that it gets
data from the right object in the array:

- (id) tableView: (NSTableView *) tv
    objectValueForTableColumn: (NSTableColumn *) tc
    row: (int) row
{
    return [[records objectAtIndex: row] valueForKey: [tc identifier]];
}

Hope this helps,
Andrew

On Sunday, January 26, 2003, at 07:25 PM,
<email_removed> wrote:

> Good Folk
>
> Need a push in the right direction please.
>
> Background;
> Ive only been "programming" for about 6 weeks.That being said Im quite
> happy with the way its going and having a blast doing it.
> At the moment Im really only taking someone elses C code and changing
> it to suit my purpose in Project Builder.
>
> For the record
> OS = 10.2.3
> Dev Tools = Dec 2002
>
> The basic exercise is to take some pre-written functions that
> interrogate a database and send the result to a table.The snippet
> below appears to work just fine. It talks to the DB, gets 1 row x 11
> columns and populates the table as required dynamically changing the
> Key Name with each pass.
> Table looks like demo data at bottom of page with only the first
> row.This is good.
>
>
>
>  NSMutableDictionary *record = [NSMutableDictionary dictionary];
>    nFields = PQnfields(res);
>
>    for (i = 0;i < nFields; i++)
>    {
>
>    paramNames = [[NSMutableString
> alloc]initWithCString:PQfname(res,i)];
>    [record setObject:paramNames forKey:paramNames];
>    NSLog(@"Adding paramNames to Dictionary %@",record);
>
>    }
>    NSLog(@"About to add paramNames to Array");
>    [records addObject:record];
>
>
>
>     
> Its here that things get unpleasant and have caused me many tears.
> As you can see Ive been NSLogging frantically and have the output if
> required.The bottom line is this..
> The loop below will run 4 times hopefully each time adding a distinct
> dictionary to the array.
> The line added by the method above ALWAYS stays put and visible in the
> resultant table
> datname        datdba        encoding      datistemplate 
> datallowconn  datlastsysoid  datvacuumxid  datfrozenxid  datpath   
>    datconfig      datacl
>
> However each time I add a dictionary from the loop/s below it replaces
> the one that went before so after two exterior loops Id see
> datname        datdba        encoding      datistemplate 
> datallowconn  datlastsysoid  datvacuumxid  datfrozenxid  datpath   
>    datconfig      datacl
> rodkirkp      100            0              f              t         
>    16974          427            427
> rodkirkp      100            0              f              t         
>    16974          427            427
>
> NSLogging the contents of the array shows only one iteration of the
> data but it is sent to the table for as many times as there have been
> loops
>
>
>
>  NSMutableDictionary *data = [NSMutableDictionary dictionary];
>    int temp;
>    i = 0;
>    temp = 0;
>
>
>    for (i = 0; i < PQntuples(res); i++){
>
>    for (j = 0; j < nFields; j++)
>     
>    {
>    NSLog(@"Populating Dictionary %i",j);
>    fieldData = [[NSMutableString alloc]
> initWithCString:PQgetvalue(res,i,j)];
>    dynamicKeyName = [[NSMutableString
> alloc]initWithCString:PQfname(res,temp)];
>    [data setObject:fieldData forKey:dynamicKeyName];
>    NSLog(@"%i Adding %@ forKey %@ to Dictionary
> data",j,fieldData,dynamicKeyName);
>    temp++;
>    }
>     printf("\n \n");
>     NSLog(@"Adding dictionary to array");
>     NSLog(@"Dictionary equals %@",data);
>     [records addObject:data];
>     NSLog(@"The array contents are now %@", data);
>     temp = temp-temp;
>
>
>
>
>    }
>
>    [tableView reloadData];
>
>
>
>
> Demo Data (What it SHOULD look like - sorta)
> datname        datdba        encoding      datistemplate 
> datallowconn  datlastsysoid  datvacuumxid  datfrozenxid  datpath   
>    datconfig      datacl
> mydb               100               0                  f                     t   
>             16974          427            427
> rodkirkp      100            0              f              t         
>    16974          427            427
> template1      1              0              t              t         
>    16974          427            427                                 
>        {=,postgres=CT}
> template0      1              0              t              f         
>    16974          427            427                                 
>        {=,postgres=CT}
>
>
>
> What Im after
> I get that the above is probably very messy and there may be a neater
> way of doing it but thats not the point.I need to know, in very simple
> terms why the above doesnt work.If you find yourself asking "Do I need
> to draw this guy a picture ?" the answer is probably yes :-)
> Saying that I need to re-arrange my ids or re-allocate my dictionaries
> wont cut it either.Could you PLEASE show me.
> I have a feeling that Im having a "retain"/memory management issue
> here but not sure how to fix it - if its the issue


Related mailsAuthorDate
mlPlease help a newbie in distress rodkirkpatrick Jan 26, 19:24
mlRe: Please help a newbie in distress Andrew Zamler-Carh… Jan 26, 21:39