saving variables to a file

Discussion in 'iOS Programming' started by liptonlover, Sep 24, 2008.

  1. liptonlover macrumors 6502a

    Joined:
    Mar 13, 2008
    #1
    I will not have actual whole objects to save, just particular variables. They will be ints, floats and doubles. How do I do this?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Easiest way is to stick them in a NSDictionary (wrapped in NSValue/NSNumber objects if required) and save that via the obvious method.
     
  3. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #3
    hmm.... looking at the class doc it seems I'll have to wrap them in an NSNumber or NSValue, then an NSArray, THEN an NSDictionary. Seems like that's taking the long way... and it doesn't help that I haven't used NSDictionary or successfully saved an object or variable before. I'll look into it more but I'd like more suggestions please.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I've no idea what you're using the array for. Create an NSMutableDictionary instance. Set each value you want to save for a sensible key. Write out via the writeToFile: method. No array...
     
  5. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #5
    Sorry I was looking at NSDictionary, not Mutable. Thanks!
     
  6. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #6
    When I don't care about tiny rounding errors, I just convert to string representation (sprintf), and write plain text files. Makes it much easier to dump the file to a text editor for debugging or printing, or for moving the data cross-platform.
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Whilst this approach is perfectly sensible it does take more code, especially to reload. We can reload the dictionary from a file with one line of code :D
     

Share This Page