數據對象(data object):
二進制數據能被包裝進Foundation和Core Foundation數據對象中,數據對象提供了操作數據的面向對象封裝。
數據對象是橋接對象(bridged objects),所以你能在Foundation和Core Foundation中自由切換。數據對象
能自動的分配和回收數據緩衝區。數據對象(data objects)還能被存儲在集合中,能被寫入屬性列表,存入文件,通過通信端口傳輸等。
數據對象的應用:
從原始字節(raw bytes)創建數據對象
一般用以data開頭的NSData或NSMutableData類方法創建包含這些原始字節的數據對象。
從文件和URLs中創建對象:
用dataWithContentsOfFile:
or dataWithContentsOfURL:類方法創建數據對象,數據對象中包含文件或者URL的內容
NSString *thePath = @"/u/smith/myFile.txt";
NSData *myData = [NSData dataWithContentsOfFile:thePath];
NSData的兩個原始bytes和length方法爲類的其他方法提供了基礎。
bytes返回一個指向包含數據的數據對象。length方法返回數據對象包含的字節數量。
NSData提供了從一個數據對象中訪問字節或者拷貝字節到指定緩衝區的方法。getBytes:方法拷貝所有的
字節到一個緩衝區。
unsigned char aBuffer[20];
NSString *myString = @"Test string.";
const char *utfString = [myString UTF8String];
NSData *myData = [NSData dataWithBytes: utfString length: strlen(utfString)];
[myData getBytes:aBuffer];
抽取一個數據對象的內容是否是另外數據對象的子集:
NSString *myString = @"ABCDEFG";
const char *utfString = [myString UTF8String];
NSRange range = {2, 4};
NSData *data1, *data2;
data1 = [NSData dataWithBytes:utfString length:strlen(utfString)];
data2 = [data1 subdataWithRange:range];
存儲數據對象:
writeToFile:atomically:把數據存儲到一個本地文件中
和 writeToURL:atomically:把數據存儲到指定的網絡地方。
可變數據對象:NSMutableData
改變bytes
NSMutableData *data1, *data2;
NSString *myString = @"string for data1";
NSString *yourString = @"string for data2";
const char *utfMyString = [myString UTF8String];
const char *utfYourString = [yourString UTF8String];
unsigned char *firstBuffer, secondBuffer[20];
/* initialize data1, data2, and secondBuffer... */
data1 = [NSMutableData dataWithBytes:utfMyString length:strlen(utfMyString)+1];
data2 = [NSMutableData dataWithBytes:utfYourString length:strlen(utfYourString)+1];
[data2 getBytes:secondBuffer];
NSLog(@"data2 before: \"%s\"\n", (char *)secondBuffer);
firstBuffer = [data2 mutableBytes];
[data1 getBytes:firstBuffer];
NSLog(@"data1: \"%s\"\n", (char *)firstBuffer);
[data2 getBytes:secondBuffer];
NSLog(@"data2 after: \"%s\"\n", (char *)secondBuffer);
在bytes後加入
NSMutableData *data1, *data2;
NSString *firstString = @"ABCD";
NSString *secondString = @"EFGH";
const char *utfFirstString = [firstString UTF8String];
const char *utfSecondString = [secondString UTF8String];
unsigned char *aBuffer;
unsigned len;
data1 = [NSMutableData dataWithBytes:utfFirstString length:strlen(utfFirstString)];
data2 = [NSMutableData dataWithBytes:utfSecondString length:strlen(utfSecondString)];
len = [data2 length];
aBuffer = malloc(len);
[data2 getBytes:aBuffer];
[data1 appendBytes:aBuffer length:len];
置換bytes
NSMutableData *data1, *data2;
NSString *myString = @"Liz and John";
NSString *yourString = @"Larry";
const char *utfMyString = [myString UTF8String];
const char *utfYourString = [yourString UTF8String];
unsigned len;
unsigned char *aBuffer;
NSRange range = {8, strlen(utfYourString)};
data1 = [NSMutableData dataWithBytes:utfMyString length:strlen(utfMyString)];
data2 = [NSMutableData dataWithBytes:utfYourString length:strlen(utfYourString)];
len = [data2 length];
aBuffer = malloc(len);
[data2 getBytes:aBuffer];
[data1 replaceBytesInRange:range withBytes:aBuffer];