Isn't the FileHandle call supposed to create the file? How else do you do it? The only other create file I can find is one that creates a file when you already have the complete contents to provide at the start, and I don't.

Okay I got this working. It's either a roundabout, screwed up way or Apple really doesn't want us writing out files incrementally. Seems braindead, because there are MANY situations where you don't want to create an entire copy of your data-to-write in memory so that you can save it out in one call. I can't believe simply writing a few bytes to a file and reading them back is so complicated in Swift but here is the only way I've found if you're not going the NSArchiver route:

I couldn't get a FileHandle to write to until I actually used Data.write to create the file -- make sense? NO! After that, you cannot continue to use data.write, like any normal person would think, because apparently data.write does not append, it only overwrites at the beginning of the file. Surely there is some way to do this that I am missing?

THEN, to write more data, you can get FileHandle forUpdatingAtPath because you've now created the file by writing out one piece of data. Thereafter, you can use file.write(data) to write additional items to the file. BUT WAIT - make sure you call file.seekToEndOfFile() before every write, or it won't move past the last thing you wrote.

Sheez -- so complicated to do something so simple -- someone please illuminate me if there's an easier way. And I'm just talking about old-school writing bytes to files sequentially. I am not talking about writing all my data to a file at once, which is easy if you have all your data in one place. And not NSArchiver which is not practival sometimes.

However -- because this is so screwy I am going to go back and look into reconfiguring my data usage so that I can use NSArchiver, although I didn't initially want to do that.

More Like This

Retrieving data ...

This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Developer Forums Participation Agreement.