objective c - Convert NSValue to NSData and back again, with the correct type -
i able convert objective-c object, such nsarray
or uiimage
nsdata
object, can use write disk. first converted them nsvalue
, planned on converting nsdata
. this question provided part of answer, working nsnumber
, didn't have need convert nsvalue
.
i have seen other questions such this one relies on nskeyedarchiver
, steer away due vast size inflation occurs.
therefore code @ moment encoding nsdata
object nsvalue
, first question, follows:
+(nsdata*) datawithvalue:(nsvalue*)value { nsuinteger size; const char* encoding = [value objctype]; nsgetsizeandalignment(encoding, &size, null); void* ptr = malloc(size); [value getvalue:ptr]; nsdata* data = [nsdata datawithbytes:ptr length:size]; free(ptr); return data; }
my question how go decoding nsdata
object has been encoded in manner , original objctype
nsvalue
?
i assume using along these lines
[nsvalue valuewithbytes:[data bytes] objctype:typehere];
how type information?
use nskeyedarchiver
save items , nskeyedunarchiver
restore object. object must conform nscoding
, in case of collection contained items must conform nscoding
.
see apple documentation of nskeyedarchiver , nscoder
Comments
Post a Comment