objective-ccore-datansdatansvalue

Convert NSValue to NSData and back again, with the correct type


I would like to be able to convert an objective-c object, such as an NSArray or UIImage into an NSData object, which I can then use to write to disk. I first converted them to an NSValue, which I then planned on converting to NSData. This question provided part of the answer, but as they were working with NSNumber, they didn't really have a need to convert it back to NSValue.

I have seen other questions such as this one that relies on NSKeyedArchiver, but I would like to steer away from this due to the vast size inflation that occurs.

Therefore my code at the moment for encoding an NSData object from an NSValue, from the first question, is as 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 is how would I go about decoding an NSData object that has been encoded in this manner and get back the original objCType from the NSValue?

I would assume I would be using something along these lines

[NSValue valueWithBytes:[data bytes] objCType:typeHere];

How would I get back the type information?


Solution

  • Use NSKeyedArchiver to save the items and NSKeyedUnarchiver to restore the object. The object must conform to NSCoding and in the case of a collection all contained items must also conform to NSCoding.

    See the Apple documentation of NSKeyedArchiver and NSCoder