Search code examples
iosobjective-cmacosnsdatanskeyedarchiver

Struct to NSData between Mac and iOS


I have been working on this problem for a while, I have an app running on the mac, it has co-ordinate data stored in a struct like this:

struct xyz {
  float x;
  float y;
  float z;
};

struct xy {
  float x;
  float y;
};

struct object {
  struct xyz *myXYZ;
  struct xy *myXY;
};

This all works as expected, then I add the struct into NSData like so:

struct object anInitialTestStruct;
NSMutableData *myTestDataOut = [NSMutableData dataWithBytes:&anInitialTestStruct length:64 freeWhenDone:NO];
BOOL good = [myTestDataOut writeToFile:[NSString stringWithFormat:@"%@/filename.dat", docsDirectory] atomically:YES];

This works as expected, I get a file and looks like there is data in it (for reference I have used pointers and malloc for the anInitialTestStruct but still don't get the desired result)

Now on the iphone, I copy the file into the project, and do this:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"dat"];
NSData *myVecNSData = [[NSData alloc] initWithContentsOfFile:filePath options:NSDataReadingUncached error:&error];
if ( error ) {
    NSLog(@"%@", error);
}

I don't get the correct data back. Interestingly if I run the initWithContents method on the mac and read the file in there it appears to be ok.

So I'm thinking there is something different on the iphone / mac way it deals with the filesystem.... I've tried encoding the data using NSKeyedArchiver, but I get an exception stating "incomprehensible archive....."


Solution

  • 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];