views:

50

answers:

2

I am trying to check whether a plist exists in my doc folder: if yes, load it, if not load from resource folder.

 - (void)viewWillAppear:(BOOL)animated
 {
 //to load downloaded file
 NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
 NSString *documentsDirectory = [docpaths objectAtIndex:0]; 
 NSString *docpath = [documentsDirectory stringByAppendingPathComponent:@"downloadedfile.plist"];  

 //if document folder got file
  if(docpath != nil) 
   {
   NSDictionary *dict = [[NSDictionary alloc] 
     initWithContentsOfFile:docpath];
   self.allNames = dict;
   [dict release];
  }
  //on error it will try to read from disk 

  else {
  NSString *path = [[NSBundle mainBundle] pathForResource:@"resourcefile"
           ofType:@"plist"];
   NSDictionary *dict = [[NSDictionary alloc] 
     initWithContentsOfFile:path];
    self.allNames = dict;
   [dict release];

   } 
   [table reloadData];

Where did I go wrong? The plist is not loading from resource folder.

A: 

I think if you create an instance of NSFileManager you can then use the file exists method

BOOL exists;
NSFileManager *fileManager = [NSFileManager defaultManager];

exists = [fileManager fileExistsAtPath:docPath];

if(exists == NO)
{
// do your thing
}
Chris
A: 

You need to check wether the file exists in your Documents folder (with NSFileManager or something like this). stringByAppendingPathComponent: doesn’t care wether the path it returns exists or is valid.

Sven