Gianni
Gianni

Reputation: 51

problem with NSKeyedUnarchiver unarchivedObjectOfClass: fromData:error;

NSMutableArray * array;
array = [NSKeyedUnarchiver unarchiveObjectWithFile: mypath];

it is OK.

NSData * data = [NSData dataWithContentsOfFile: mypath];
array = [NSKeyedUnarchiver unarchiveObjectWithData: data];

it is OK.

The method I can't use is:

+ UnarchivedObjectOfClass:``` from Date: error:

    @interface Magazzino: NSObject <NSCoding> {
    NSNumber * num;
    NSString * description;
    NSString * um;
    NSNumber * price list;
    NSNumber * discount;
    NSNumber * cost;
    NSNumber * price increase;
   }

NSMutableArray * array;
array = [NSKeyedUnarchiver unarchivedObjectOfClass: Magazzino.class   fromData: data error: & error];

Error Domain = NSCocoaErrorDomain Code = 4864 "value for key 'root' was of unexpected class' NSMutableArray '. Allowed classes are' {(      Magazzino

Upvotes: 3

Views: 1641

Answers (1)

swainwri
swainwri

Reputation: 64

I found you have to include the NSString and NSNumber as well as the main class in an NSSet.

Eg

NSSet *classesSet = [NSSet setWithObjects:[NSString class], [SurveyInfo class], [SurveyData class], [PlotInfo class], [DepthRange class], [SurveyLegData class], [ToolfaceData class], [WellboreGeometry class], [WellboreGeometryItem class], [AxisTitles class], [AxisLabels class], [AxisDimensions class], [TextStyle class], [Cell class], [UIFont class], [UIColor class], [NSArray class], [NSMutableArray class], [NSMutableData class], [NSDictionary class], [NSDate class], [NSValue class], [NSNull class], nil];
            NSArray *dataArray = (NSArray*)[NSKeyedUnarchiver unarchivedObjectOfClasses: classesSet fromData: unarchivedData error: &error];

The only problem I have now is leakage.

Upvotes: 2

Related Questions