Serialization?

Member
Posts: 241
Joined: 2008.07
Post: #1
Pointers are great until it comes to needing to write the data that they point to to a file, especially when some of that data that they point to happens to be more pointers and more pointers within that, etc.

NSData performs a shallow write. I need to do a deep write. I can see that I could write a method that will write primitive data and then tell the pointers to write until there are no more pointers, however...

The problem with that is, NSData has no way to open a file and leave it open for appending, that I could tell. I got to looking at CFData functions and my head started to spin. Not sure if that's the right way to go or not.

I could just use an fstream but then I'd have to change most of my files to .mm...

Any advice as to what's the standard way of writing ALL of the data in a class to file? Thanks again!!! Grin
Quote this message in a reply
⌘-R in Chief
Posts: 1,181
Joined: 2002.05
Post: #2
If by class you mean an Obj-C class, then use actual serialization with initWithCoder:, encodeWithCoder:

http://developer.apple.com/iphone/librar...iving.html
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #3
Yes, I mean Objective-C. Thanks!
Quote this message in a reply
Moderator
Posts: 1,554
Joined: 2003.10
Post: #4
bmantzey Wrote:I could just use an fstream but then I'd have to change most of my files to .mm...

fopen, fwrite, fclose are all in C stdlib, and well worth getting comfortable with.
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #5
If you want to open a file and repeatedly append to it while staying within Cocoa, use NSFileHandle.

But yes, you should be doing this with NSCoder and friends.
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #6
I'm well versed with the fstream classes. I'm trying to learn Cocoa because there's a plethora of convenience classes out there. I'll check out the NSCoder stuff. Thanks again!
Quote this message in a reply
Moderator
Posts: 1,554
Joined: 2003.10
Post: #7
bmantzey Wrote:I'm well versed with the fstream classes.

fstream is C++. What I was referring to is C, and usable directly from an Objective-C source file without having to change the way you're compiling it. I just bring them up because I find them to be invaluable tools for any kind of C programming that deals with file I/O.
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #8
Hmm, sorry for jumping to what I thought you said. I'll look into it. Thanks again Took!
Quote this message in a reply
Post Reply