Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6926901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:58:41+00:00 2026-05-27T10:58:41+00:00

My code is here: -(void)loadDataFromDisk { [dict release]; NSMutableData* data = [NSMutableData dataWithContentsOfFile:[self pathForDataFile]];

  • 0

My code is here:

-(void)loadDataFromDisk
{
    [dict release];
    NSMutableData* data = [NSMutableData dataWithContentsOfFile:[self pathForDataFile]];
    NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    dict = [[unarchiver decodeObjectForKey:fileKey] retain];
    [unarchiver finishDecoding];
    [unarchiver release];

    if(dict == NULL)
    {
    NSLog(@"First time in.");
    dict = [[NSMutableDictionary alloc] init];
    }
}

-(void)saveDataToDisk
{
    NSMutableData* data = [NSMutableData data];
    NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

    [archiver setOutputFormat:NSPropertyListBinaryFormat_v1_0];

    // EncodeObject will automatically encode every primitive inside 'dictionaryToSave', 
    // As for objects, the object class must conform to NSCoding Protocol, else an error will occur.
    [archiver encodeObject:dict forKey:fileKey];
    [archiver finishEncoding];
    [archiver release];

    [data writeToFile:[self pathForDataFile] atomically:YES];    
}

-(NSString*)pathForDataFile
{
     NSArray* documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString* path = nil;
    if (documentDir) 
    {
        path = [documentDir objectAtIndex:0];    
    }
    else
    {
        NSLog(@"error in [PersistentHandler pathForDataFile]");
    }
    // Returns 'directory'/data.bin to method(saveDatatoDisk/loadDataFromDisk)
    return [NSString stringWithFormat:@"%@/%@", path, @"data.bin"];
}

It works if I save a file once with a specified fileKey (i.e: “buttons”).. BUT then if I want to save another file to disk with a specified fileKey (i.e: “clocks”), it would overwrite “buttons” with “clocks”, so “buttons cannot be accessed again..

How to fix this dilemma? I believe it should be how I “saveDataToDisk” (archive), I should be writing a bunch of files to disk, instead of replacing each one by the most current one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:58:42+00:00Added an answer on May 27, 2026 at 10:58 am

    Unless I am reading your code wrong, you are always using the same path to save the file to. That is why it is overwriting.

    return [NSString stringWithFormat:@"%@/%@", path, @"data.bin"];
    

    Every time you save the file is at /path/you/retrieve/data.bin

    You need to change the filename, or, the directory that it is saved in. If you want to keep the same filename then make a new directory every time you save and just label the directory with a time stamp or a description of what you are saving.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got this line of code here but its not working. private void Button_Click(object sender,
Here's the code in AlertTableView: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSInteger index = 12345; NSLog(@AlertTableView:
Here is my code : public static void main(String[] args) { // System.setProperty( //
Here’s my code behind: protected void gvviewapproval_RowUpdated(object sender, GridViewUpdatedEventArgs e) { using (SqlConnection cn
Here is my code: partial void OnisApprovedChanging(bool value) { this.dateApproved = DateTime.Now; } 'dateApproved'
Here is my code: - (void)loadView { //hard coded array of content for each
Here is the code: class Time { public static void printTime (int hour, int
Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
Here's the sample code: public static void col (int n) { if (n %
Here's the relevant code: public static void printBoarders (Territory x) { int t =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.