Search code examples
objective-ciossavenskeyedarchiver

NSKeyedArchiver working in Simulator but not Device, iOS SDK


I tried to modify it a bit and looked at other options here and googled some stuff but nothing seems to work.

here's what I have, like I said, It works perfectly in the simulator but not the device, witch runs iOS 5.x I thanks for all the help.

   NSFileManager *filemgr;
   NSString *docsDir;
   NSArray *dirPaths;

   filemgr = [NSFileManager defaultManager];

   // Get the documents directory
   dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);

   docsDir = [dirPaths objectAtIndex:0];

  //Build the path to the data file
  datafilePath = [[NSString alloc] initWithString: [docsDir 
                                                 stringByAppendingPathComponent:  @"data.archive"]];
   //  NSString *localPath = @"Documents/data.archive";
   //  datafilePath = [NSHomeDirectory() stringByAppendingPathComponent:localPath];

  // Check if the file already exists
  if ([filemgr fileExistsAtPath: datafilePath])
  {
    NSMutableArray *dataArray;
   // NSDictionary *stuff;

    dataArray = [NSKeyedUnarchiver 
                 unarchiveObjectWithFile: datafilePath];


    for(int i=0; i< [dataArray count]; i++){
    titlestring = [dataArray objectAtIndex:i ];
    detailsstring = [dataArray objectAtIndex:++i];

   // date = [dataArray objectAtIndex:2];


        [tabledata addObject:titlestring];
    [tablesubtitles addObject:detailsstring];

    }

   }





  }

this is the saveButton action: Like I said, I works fine in the simulator, just doesn't do anything in the device, everything shows up blank.

if (i == 0) {

   // [userDefaults setObject:titlefield.text forKey: @"titlehomework"];
  // [userDefaults setObject:detailstextfield.text forKey:@"detailshomework"];

    NSMutableArray *contactArray;
   contactArray = [NSKeyedUnarchiver 
       unarchiveObjectWithFile: datafilePath];

   // contactArray = [[NSMutableArray alloc] init];
    [contactArray addObject:titlefield.text];
    [contactArray addObject:detailstextfield.text];

    [NSKeyedArchiver archiveRootObject: 
     contactArray toFile:datafilePath];

    // NSDictionary *stuff;



    }

Solution

  • It's difficult to answer the question without knowing what's failing, but it may help you to know that very often when people get code working on the simulator but not on the device, the problem is related to file names. The file system in iOS is case sensitive, but the MacOS X file system often isn't case sensitive. So if you're trying to read a file with the name "data.archive" but the name of the actual file is "Data.archive", it'll work on the simulator but not on the device.