1

My custom object contains an array of unichar primitives. However there is no encodeUnichar:ForKey method in NSCoder. My solution is to transform my 2d unichar array into an NSString and then encode that.

However, the absence of a method to encode character makes me feel like I am missing something obvious, especially because I couldn't find the answer on Stack Overflow.

Before I embark on my NSString method, does anyone know if there is a simpler way to do this built in into the NSCoding protocol?

Thanks Lee.

1 Answer 1

1

A C array of fixed-size data is best encoded using encodeBytes:length:forKey:. See Encoding and Decoding C Data Types for full details.

Sign up to request clarification or add additional context in comments.

3 Comments

This will work great until you try to decode the value on a machine with a different endianess. I know, all Apple machine are little-endian but that may change in the future (it already has).
You should swap the bytes before coding and after decoding with NSSwapHostShortToLittle and NSSwapLittleShortToHost or (NSSwapHostShortToBig and NSSwapBigShortToHost if you prefer big-endian).
This works great as long as you remember that a unichar is 2 bytes long!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.