Saving Printer Setup

I have a problem saving the SetupString returned from PrinterSetup.PageSetupDialog in Cocoa.

In my program I use a class ‘XPrefs’ which uses XMLDocument and Dictionarys to store Preferences.
If I try to store the Printer SetupString as a String Preference the program crashes and the Preference File is empty.

In Cocoa, I am able to store and retrive the SetupString in a Static String Variable or in a pure TextFile - that works.
Saving the Printer SetupString in the very same ‘XPrefs’ class compiled in Carbon is also working without problems.

What I’m missing ?
How can I store the Printer SetupString with NSUserDefaults ?


Perhaps it has something to do with the fact the string is binary. Try to encode it with Base64 before writing it.

What encoding is set on the string that is returned. I suspect the XMLDocument requires UTF-8 and is very prone to crashing if provided with anything else. If the encoding is nil that would cause the problem.

Exactly what Massimo says, the printer setup string contains XML and raw bytes, so you need to encode it first.

Thank you guy’s, I will give it a try.

Ok, the SetupString returned from PrinterSetup.PageSetupDialog has Encodings UTF-8.

Massimo’s suggestion makes it working:
saving: EncodeBase64(SetupString)
retriving: DecodeBase64(SetupString)

I not really understand why as the returned SetupString is UTF-8 and Preference Files use Encoding UTF-8

It probably has nothing to do with encoding, but rather the contents of the setupstring not being xml friendly. Base64 solves that.

If it claims to be UTF-8 but the byte sequences are not actual UTF-8 characters then that will cause a problem. Just because something is labelled as UTF-8 doesn’t mean it really is UTF-8.