Basic question about how to write to a file

Discussion in 'iOS Programming' started by Japi, Jan 29, 2010.

  1. Japi macrumors newbie

    Nov 15, 2006
    I have a very basic question about how to a write a NSString to a file. I have tryed the following way, but it doesn't seem to work:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:mad:"Gameinf" ofType:mad:"txt"];
    NSString* st = @"a1";
    [st writeToFile:filePath atomically:FALSE encoding:NSUTF8StringEncoding error:nil];

    And also just:

    NSString* st = @"a1";
    [st writeToFile:mad:"Gameinf.txt" atomically:FALSE encoding:NSUTF8StringEncoding error:nil];

    I would just want to save the game level, where the player. So that the player could continue from that level, when he restarts the program.

    Any help would be appreciated.
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16)

    An easier way to do this is just to use NSUserDefaults.
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    There is a reason for that error parameter. Why don't you look at its contents?
  4. Japi thread starter macrumors newbie

    Nov 15, 2006
    Thanks for the info, dejo! I didn't know about that before (just started iphone developing). It worked like a charm.

    Just a bit confused, why didn't the write to a file work. I tested a code that read from a file, and it worked just fine.

Share This Page