views:

15

answers:

1

Here's my code of generating data

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application 
    [array initWithCapacity:20];
}

- (IBAction) readlog:(id)sender {
    for (int i = 0; i < 20; i++) {      
        NSDictionary *d = [NSDictionary dictionaryWithContentsOfFile:[path stringByAppendingFormat:@"/%d.log",i]];
        [array addObject:d];
    }

}

- (IBAction) writelog:(id)sender {
    for (int i = 0; i < 20; i++) {
        NSMutableDictionary *d = [NSMutableDictionary dictionary];
        NSString *name = [NSString stringWithFormat:@"testfile%d", i];
        [d setObject:[NSDate date] forKey:@"date"];
        [d setObject:[path stringByAppendingFormat:@"/%d.log", i] forKey:@"path"];
        [d setObject:name forKey:@"name"];
        [d writeToFile:[path stringByAppendingFormat:@"/%d.log", i] atomically:YES];
    }

and I bind my tableview column with appdelegate.array with keypath name/path/date but it doesn't show any data in the array.. is there anything wrong here? Thanks!

+1  A: 

You haven't created an array.

init methods, including NSMutableArray's initWithCapacity:, initialize an existing (freshly-created) instance. You haven't created one, so you're sending that initWithCapacity: message to nil, which means it has no effect.

You need to create the array, then initialize it, then assign it to your array variable, preferably all in the same line.

There's also the issue that your table view will have already asked for the array by the time you receive the applicationDidFinishLaunching: message. You don't have one yet, so it gets nothing; by the time you create one, it has already asked you for it and gotten its answer, and does not know that it should ask again.

Create your array in init or initWithCoder: (I believe you will need the latter if your app delegate is in a nib), and implement and use Key-Value-Coding-compatible accessor methods to fill the array with values. When you send yourself accessor messages, you'll cause KVO notifications that will tip off the table view that it needs to ask for the array again. Assigning directly to the instance variable will not cause this effect.

A couple of other things…

You have three [path stringByAppendingFormat:@"/%d.log", i] expressions in two different methods. Don't repeat yourself. Move that to a method named something like logFileNameWithLogFileNumber: and send yourself that message to generate the filename. This will make the code both clearer and easier to maintain.

Finally, as a matter of style, you should not use stringByAppendingFormat: or stringWithFormat: to construct paths. Use stringByAppendingPathComponent: (in this case, together with stringWithFormat: to generate the filename). Clarity and pathname-separator-independence are virtues.

Peter Hosey
Thank you so much!!!!!!
Frost