Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I have been bug searching the whole day and I found the problem but I don't know what is causing it. I have an IF statement that it skips since it can't find the property list. Upon entering a new viewController I have it read in the property list
Code:
- (void)viewWillAppear:(BOOL)animated
{
    if (clientContentArray) { //test line to find problem
        clientContentArray = nil;
    }
    [self readPlist];
    [super viewWillAppear:animated];
}

I have one Method that creates the path to the file
Code:
- (NSString *) dataFilePath
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    
    return [documentsDirectory stringByAppendingPathComponent:@"clientList.plist"];
}

Now in the next code I have the line • if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) • Looks like the IF statement is returning a FALSE since it is not running the code under it or printing the NSLog test that I set up. I have been cleaning up my code and moving things around so something go messed up, it was working fine last night. Does this code look right, or am I missing something?

Code:
- (void)readPlist{
    NSString *filePath = [self dataFilePath];
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        NSLog(@"I saw it");
        clientContentArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
        clientListForTable = [[NSMutableArray alloc] init];
        client = [[NSMutableDictionary alloc]init];        
        
        if ([clientContentArray count] != 0) {
            NSSortDescriptor *firstDescriptor = [[NSSortDescriptor alloc] initWithKey:@"clientNames" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
            
            [clientContentArray sortUsingDescriptors:[NSArray arrayWithObject:firstDescriptor]];
            
            for (int i = 0; i < clientContentArray.count;  i++) {
                NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:[clientContentArray objectAtIndex:i]];
                [clientListForTable addObject:[tempDict objectForKey:@"clientNames"]];
            }
        }
    } 
    [self.tableView reloadData];
}
 
Debugging Rule #5: When the unexpected happens, show what you expected.

This rule applies to posting, but it's also one of the 10 basic rules of debugging. If you expected to see a file, print the full pathname of the file you expected to see. If you expected an input to happen, print what you got and what you expected.


Debugging Rule #3: Show interesting values.

Your NSLog shows nothing interesting. The filePath is something I'd consider interesting.

Worse, you have no 'else' part, so if the file doesn't exist, you have no useful information about what pathname it was expecting to find.
 
Worse, you have no 'else' part, so if the file doesn't exist,

And that was the problem. Such a simple thing that cost me the better part of a day. While doing a little rewriting and debugging I removed the 'else' so it never wrote the file in the first place. I get so into problem solving that I forget to stop, step back, and look at the bigger picture.

Thanks Chown33
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.