2008-12-31 71 views
1

說我有一個二進制文件(用Java生成)包含一個32位的int值。我目前使用下面的Objective-C代碼用於解析它:在Cocoa Touch中解析二進制文件的最簡單方法是什麼?

NSString *path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"dat"]; 
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path]; 

unsigned long intValue; 
memcpy(&intValue, [[file readDataOfLength:4] bytes], 4); 
intValue = NSSwapBigLongToHost(intValue); 

[file closeFile]; 

我的第一個問題是問,如果是做對iPhone的事情,因爲我沒有找到任何接近Java的DataInputStream類的常見方式。

我的第二個問題是關於帶有memcpy的代碼行。我不爲什麼已瞭解以下部分(更優雅,少了「低級別」)不代替工作:

[[file readDataOfLength:4] getbytes:&intValue]; 

我得到上構建一個警告:

'NSData' may not respond to '-getbytes:' 

在執行,我得到:

'NSInvalidArgumentException', reason: '*** -[NSConcreteData getbytes:]: unrecognized selector sent to instance 

回答

2

對於最後一個問題,請使用getBytes(大寫字母B)。

相關問題