[Cocoa/Obj.C] Hoe vul je een NSDictionary?

Pagina: 1
Acties:

  • Reinder83
  • Registratie: September 2002
  • Laatst online: 19-02 13:41
Hallo,

ik ben al een tijdje aan het proberen, maar ik wil data wat ik uit mysql fetch in een NSDictionary zetten, maar wat ik ook probeer die blijft leeg.

ik dan ook nog met een aantal vragen kan je aan een key meerdere waarden toekennen?
ik wil die NSDictionary koppelen aan een NSTableView

ff wat code, dit is wat ik tot nu toe heb geprobeerd (de sql query gaat goed)
C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// fetches data of previous query (SELECT query)
- (NSDictionary*) fetchData {

    NSDictionary* returnDict;
    NSString*     value;
    NSString*     field;
    
    MYSQL_RES*    result;      // for storing the result of the query
    MYSQL_ROW     row;         // used to store the fetched data
    MYSQL_FIELD*  fields;      // for fetching field names
    
    unsigned int  columns;     // keeps number of fields (columns)
//  my_ulonglong  rows;        // for keeping number of records
    int           i;           // used as counter in for-loops
    
    result = mysql_store_result(_mysql); // create resource for query
    
    fields = mysql_fetch_fields(result); // fetch field names
    columns= mysql_num_fields(result);   // get number of columns
//  rows   = mysql_num_rows(result);     // get number of rows
    
    
    returnDict = [NSDictionary dictionary];

    // set keys for dictionary
/*  for(i=0; i<columns; i++) {
        field = [NSString stringWithFormat:@"%s", fields[i]];
        [returnDict set
    }
*/  
    
    // fetch data into the array
    while(row = mysql_fetch_row(result)) {
        
        for(i=0; i<columns; i++) {          
            // fill columns
            value = [NSString stringWithFormat:@"%s", row[i]];
            field = [NSString stringWithFormat:@"%s", fields[i]];
            NSLog(@"column: %@,\tvalue: %@", field, value);
            if(i == 0) {
                [returnDict setValue: value forUndefinedKey: field];
            } else {
                [returnDict setValue: value forKey: field];
            }
            
        }
        NSLog(@"---");
    }

    mysql_free_result(result);  
    return returnDict;
}


dit is de foutmelding:
code:
1
2
2006-03-28 10:38:33.583 CHStore[606] column: tag_id,    value: 1
2006-03-28 10:38:33.585 CHStore[606] [<NSCFDictionary 0x381ad0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tag_id.


Hoop dat iemand me wat verder kan helpen, en me kan vertellen hoe je een NSDictionary vult.

  • Reinder83
  • Registratie: September 2002
  • Laatst online: 19-02 13:41
Na een tijdje zoeken heb ik iets bruikbaars gevonden, het blijkt dat ik een NSMutableDictionary nodig heb.
http://www.otierney.net/objective-c.html#nsdictionary

nu kan ik weer vooruit :)