2013-04-05 30 views
0
之間的NSData

我一直在這個問題上一段時間,我對Mac上運行的應用程序,它存儲在一個結構類似這樣的座標信息:結構到Mac和iOS

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

struct xy { 
    float x; 
    float y; 
}; 

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

這一切都按預期工作,然後我添加了結構爲NSData像這樣:

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

可正常工作,我得到一個文件,看起來像在它裏面的數據(參考我用指針和malloc的對anInitialTestStruct但仍然沒有得到期望的結果)

現在在iPhone上,我將文件複製到項目,並做到這一點:

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

我沒有得到正確的數據備份。有趣的是,如果我在mac上運行initWithContents方法,並在那裏讀取文件,它似乎是好的。

所以我想在iphone/mac的方式有不同的處理文件系統....我試過編碼數據使用NSKeyedArchiver,但我得到一個異常,指出「難以理解的存檔... ..「

+0

我想主要的問題是你正試圖在結構上使用對象方法。嘗試將結構包裝在NSValue中,稍後嘗試在NSdata中隱藏並寫入文件。當然要記住,當你恢復文件時,你會得到一個NSValue,你需要提取結構。 – Andrea 2013-04-05 07:37:34

+0

謝謝安德烈,我試圖把它包裝在一個NSValue中,這似乎很好(雖然我寫它時,文件大小隻有8個字節)。我很難將數據恢復到NSValue中,我們是否將數據讀入NSData然後將其轉換爲NSValue?不太確定如何做到這一點,會閱讀並看看我能得到多遠。 – Darren 2013-04-05 15:28:08

回答

1

爲了您的「對象」結構的情況下,您可以選擇存儲「XY」和「XYZ」的結構分開,例如在詞典:

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

和解碼是這樣的:

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]; 
+0

謝謝,我喜歡這個答案,保持數據的結構。我實際上已經得到了我的要求。我將很快發佈代碼以供比較。 – Darren 2013-12-03 11:27:05

0

你可能有truble編碼的指針see here

」指針

不能編碼指針,並取回一些有用的東西,在解碼時,你必須將信息編碼到該指針指向,這在非鍵控編碼中也是如此......「

+0

嗨,謝謝丹,它不是我一直有問題的編碼,我正在使用編碼來試圖解決我的問題。基本上我想從我的iPhone項目中獲取數據從一個mac到一個文件。保存在Mac上並在Mac上重新打開的數據顯示正常,但是當數據文件複製到我的iphone項目時,數據不正確。我爲此使用NSData,但我現在hainvg問題得到NSData和NSValue共享上述Andrea的評論相同的數據。 – Darren 2013-04-06 05:14:15