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 967391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:18:32+00:00 2026-05-16T02:18:32+00:00

Im just having trouble reading and writing NSString to a dat file on the

  • 0

Im just having trouble reading and writing NSString to a dat file on the iphone.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *saveFile = [documentsDirectory stringByAppendingPathComponent:@"Profiles.dat"];
  Boolean saveFileExists = [[NSFileManager defaultManager] fileExistsAtPath:saveFile];

  // NSString *saveFile = [documentsDirectory stringByAppendingPathComponent:@"Profiles.dat"];


  NSMutableData *gameData;
  NSKeyedUnarchiver *decoder;

  NSKeyedArchiver *encoder;

  //decoder  = [[NSKeyedUnarchiver alloc] initForWritingWithMutableData:gameData];

  myGameState *gameState = [myGameState sharedMySingleton];


  if(saveFileExists) {
   gameData = [NSData dataWithContentsOfFile:saveFile];
   decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];

   gameState.Profile1 = [decoder decodeObjectForKey:@"Name1"];
   gameState.Profile2 = [decoder decodeObjectForKey:@"Name2"];
   gameState.Profile3 = [decoder decodeObjectForKey:@"Name3"];
   gameState.Profile4 = [decoder decodeObjectForKey:@"Name4"];
   gameState.Profile5 = [decoder decodeObjectForKey:@"Name5"];

   gameState.curProfile = [decoder decodeObjectForKey:@"curProfile"];
   [decoder release];


  }
  else {
   gameData = [NSMutableData data];
   encoder  = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameData];
   gameState.Profile1 = @"Default1";
   gameState.Profile2 = @"Default2";
   gameState.Profile3 = @"Default3";
   gameState.Profile4 = @"Default4";
   gameState.Profile5 = @"Default5";

   [encoder encodeObject:@"Default1" forKey:@"Name1"];
   [encoder encodeObject:@"Default2" forKey:@"Name2"];
   [encoder encodeObject:@"Default3" forKey:@"Name3"];
   [encoder encodeObject:@"Default4" forKey:@"Name4"];
   [encoder encodeObject:@"Default5" forKey:@"Name5"];

   gameState.curProfile = gameState.Profile1;
   [encoder encodeObject:gameState.curProfile forKey:@"curProfile"];

   [encoder finishEncoding];
   //[gameData writeToFile:<#(NSString *)path#> atomically:<#(BOOL)useAuxiliaryFile#>
   [gameData writeToFile:saveFile atomically:YES ];
   //[gameData writeToFile:saveFile atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

   [encoder release];

  }

gameState.curProfile etc are all NSString pointers. Now im very new to Objective C and just cant seem to get this working correctly. I have searched around forums and cant seem to find the correct way to do this. Literally all i want is to read a write a NSString to a dat file but it aint working as is. I have managed to write ints and bools with no problems just cant get past the text problem on my own.
any help is appreciated
thanks
g

  • 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-16T02:18:32+00:00Added an answer on May 16, 2026 at 2:18 am

    Your code works without any problem here. I guess you have problems in other parts of codes.

    By the way, if you only need to save a few entries, you don’t have to do all these works yourself; just use NSUserDefaults. It automatically prepares the file to save the data, encodes the data, and decodes the data on the next launch. See this Apple document. It can be used as

     [[NSUserDefaults standardUserDefaults] setString:@"boo" forKey:@"key!"];
    

    and later the data can be read as

     NSString* s=[[NSUserDefaults standardUserDefaults] stringForKey:@"key!"];
    

    It’s as easy as this! You might want to call

    [[NSUserDefaults standardUserDefaults] synchronize];
    

    to force the saving of the data to the file; usually this isn’t necessary (The system does this automatically when quit, etc.)

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

Sidebar

Related Questions

I'm having trouble reading and writing QByteArray data to a file. My goal is
I'm trying to remove vowels from a text file and am having some trouble.
We're having a lot of trouble tracking down the source of \u2028 (Line Separator)
Reading through the existing unit testing related threads here on Stack Overflow, I couldn't
I have a code-base that I'm looking to split up and add to by
I have a process that will be called rather frequently from cron to read
I'm a freshman in college going for my computer science degree... I've programmed plenty
Recently I have been studying recursion; how to write it, analyze it, etc. I
We Want to Run Our C# Code on the JVM My company has a

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.