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....."