Saving data

  • Hey all,

    How do I save data into a file? I have 2 books on Cocoa programming
    (Cocoa Programming with Mac OS X, and Learning Cocoa with Objective C)
    and they only show you how to do it using a Cocoa Document Application.
    Is there a way to do it with a normal single window/nib file Cocoa
    Application?

    Thanks,

    Joseph
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • on 1/27/03 11:38 PM, the method -(id)<joeyt...>:(id)
    sender:@"Joseph Tesmer"; returned:

    > Hey all,
    >
    > How do I save data into a file? I have 2 books on Cocoa programming
    > (Cocoa Programming with Mac OS X, and Learning Cocoa with Objective C)
    > and they only show you how to do it using a Cocoa Document Application.
    > Is there a way to do it with a normal single window/nib file Cocoa
    > Application?

    What are you trying to save?  For most of the Foundation storage classes
    (NSArray, NSDictionary, even NSNumber), you can just use writeToFile:
    atomically:.  For everything else, there's NSArchiver.  You may also want to
    look at NSPropertyListSerialization.

    HTH,

    --
    Isaac Sherman
    MotaSoft Software
    http://homepage.mac.com/huperzoevs/
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Joseph,

    simply do the following:

    - (void)saveDataIntoFile:(NSString*)pFilePath
    {
        char * pBytes;
        unsigned length;

        length = DATAFILE_LENGTH; //custom define
        pBytes = malloc(length);

        // store your data to pBytes here

        {    // Save to file
            NSData * pSaveData = [NSData dataWithBytes:pBytes length:length];
            [pSaveData writeToFile:pFilePath atomically:FALSE];
        }

        free(pBytes);
    }

    Hope that helps!

    Nico

    On Dienstag, Januar 28, 2003, at 07:00  Uhr, cocoa-dev-
    <request...> wrote:

    > Hey all,
    >
    > How do I save data into a file? I have 2 books on Cocoa programming
    > (Cocoa Programming with Mac OS X, and Learning Cocoa with Objective C)
    > and they only show you how to do it using a Cocoa Document Application.
    > Is there a way to do it with a normal single window/nib file Cocoa
    > Application?
    >
    > Thanks,
    >
    > Joseph
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.