Iphone – How to encode an NSDecimal

cocoa-touchiphoneuikit

I can only guess, but please correct me if I am wrong:

[encoder encodeValueOfObjCType:@encode(NSDecimal) at:&theDecimal];

where encoder is an instance of NSCoder.

But one thing is strange: Why is there no key to provide? How would I get that back then, without a key?

Best Answer

Again at the very top of NSDecimalNumber's documentation, it states that it conforms to the NSCoding protocol, which means you can put it into an NSCoder the normal way:

[encoder encodeObject:theDecimal forKey:@"aKey"];

Edit: Whoops, I see you meant NSDecimal, not NSDecimalNumber. My bad. Still, a very simple way would be to wrap the NSDecimal in an NSDecimalNumber and store it that way.