2012-09-17 18 views

回答

7

你也可以如果您需要讀取接口,請創建一個NSInputStream對象:

NSData *data = ...; 
NSInputStream *readData = [[NSInputStream alloc] initWithData:data]; 
[readData open]; 

但是,您應該知道initWithData拷貝了的數據內容。

20

How to read binary bytes in NSData?」可以幫助你:

NSString *path = @"…put the path to your file here…"; 
NSData * fileData = [NSData dataWithContentsOfFile: path]; 
const char* fileBytes = (const char*)[fileData bytes]; 
NSUInteger length = [fileData length]; 
NSUInteger index; 

for (index = 0; index<length; index++) 
{ 
    char aByte = fileBytes[index]; 
    //Do something with each byte 
} 
0

最簡單的方法之一是使用NSData getBytes。

NSData *data = ...; 
char buffer[numberOfBytes]; 
[NSData getBytes:buffer range:NSMakeRange(position, numberOfBytes)]; 

其中位置和長度是您希望從NSData讀取的位置,長度是您想要讀取的字節數。無需複製。