For case of your "object" structure you have to store "xy" and "xyz" structures separately, for example in a dictionary:
struct object anInitialTestStruct;
NSDictionary *structureDataAsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSMutableData dataWithBytes:anInitialTestStruct.myXY length:sizeof(xy)], @"xy key",
[NSMutableData dataWithBytes:anInitialTestStruct.myXYZ length:sizeof(xyz)], @"xyz key",
nil];
NSData *myTestDataOut = [NSKeyedArchiver archivedDataWithRootObject:structureDataAsDictionary];
BOOL good = [myTestDataOut writeToFile:[NSString stringWithFormat:@"%@/filename.dat", docsDirectory] atomically:YES];
and decoding is something like this:
struct object anInitialTestStruct;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"dat"];
NSData *myVecNSData = [[NSData alloc] initWithContentsOfFile:filePath options:NSDataReadingUncached error:&error];
if ( error ) {
NSLog(@"%@", error);
}
// retrieving dictionary from NSData
NSDictionary *structureDataAsDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:myVecNSData];
// allocating memory for myXY and myXYZ fields
anInitialTestStruct.myXY = (xy*)malloc(sizeof(xy));
if (anInitialTestStruct.myXY == NULL) {
// error handling
}
anInitialTestStruct.myXYZ = (xyz*)malloc(sizeof(xyz));
if (anInitialTestStruct.myXYZ == NULL) {
// error handling
}
// filling myXY and myXYZ fields with read data
[[structureDataAsDictionary objectForKey:@"xy key"] getBytes:anInitialTestStruct.myXY];
[[structureDataAsDictionary objectForKey:@"xyz key"] getBytes:anInitialTestStruct.myXYZ];