NSData全部API學(xué)習(xí)莫鸭。
大家好,我是大鵬烘嘱,在socket網(wǎng)絡(luò)編程傳輸?shù)倪^(guò)程中NSData用的地方很多,也總結(jié)了一些蝗蛙。
歡迎來(lái)我的iOS開(kāi)發(fā)群163619240交流蝇庭。
/****************? ? ? ? Base 64 Options****************/
/***Base64是一組二進(jìn)制到文本轉(zhuǎn)化的方案,以ASSCII格式表示二進(jìn)制數(shù)據(jù)捡硅,這些方案用來(lái)編碼二進(jìn)制數(shù)據(jù)以存儲(chǔ)或者通過(guò)把多媒體文件轉(zhuǎn)換成文本數(shù)據(jù)進(jìn)行傳輸哮内,這個(gè)能保證數(shù)據(jù)在傳輸?shù)倪^(guò)程中的完整性。Base64醉常見(jiàn)的用于是處理電子郵件附件病曾,或者解碼小圖片牍蜂。在iOS7之前漾根,Base64的編碼和解碼是需要自己實(shí)現(xiàn)的或者是使用第三方庫(kù)泰涂,但是現(xiàn)在蘋(píng)果提供一些API來(lái)實(shí)現(xiàn)Base64功能***/
//4.base64編碼中的一些設(shè)置選擇
typedefNS_OPTIONS(NSUInteger, NSDataBase64EncodingOptions) {
NSDataBase64Encoding64CharacterLineLength =1UL <<0,
NSDataBase64Encoding76CharacterLineLength =1UL <<1,
NSDataBase64EncodingEndLineWithCarriageReturn =1UL <<4,
NSDataBase64EncodingEndLineWithLineFeed =1UL <<5,
}NS_ENUM_AVAILABLE(10_9,7_0);
//5.base64在解碼過(guò)程中的option可選條件,下面選項(xiàng)表示在將誒嗎過(guò)程中忽略不能識(shí)別的字節(jié)
typedefNS_OPTIONS(NSUInteger, NSDataBase64DecodingOptions) {
NSDataBase64DecodingIgnoreUnknownCharacters =1UL <<0
}NS_ENUM_AVAILABLE(10_9,7_0);
/****************Immutable Data不可變的Data****************/
@interfaceNSData :NSObject
//6.獲取data長(zhǎng)度的屬性
@property(readonly)NSUIntegerlength;
//7.返回data對(duì)象的首指針
@property(readonly)constvoid*bytesNS_RETURNS_INNER_POINTER;
@end
@interfaceNSData (NSExtendedData)
//8.返回一個(gè)ASCII編碼格式的字符串,采用的格式是data屬性列表的格式辐怕。
@property(readonly,copy)NSString*description;
//9.取出data中指定長(zhǎng)度的字節(jié)存入buffer這個(gè)提前聲明的數(shù)組中
- (void)getBytes:(void*)buffer length:(NSUInteger)length;
//9.取出data中指定位置range的字節(jié)存入buffer這個(gè)提前聲明的數(shù)組中
- (void)getBytes:(void*)buffer range:(NSRange)range;
//10.判斷兩個(gè)data是否相等
- (BOOL)isEqualToData:(NSData*)other;
//11.截圖data指定位置的子data
- (NSData*)subdataWithRange:(NSRange)range;
//12.將data寫(xiě)入指定的文件
- (BOOL)writeToFile:(NSString*)path atomically:(BOOL)useAuxiliaryFile;
//13.將data寫(xiě)入指定的url
- (BOOL)writeToURL:(NSURL*)url atomically:(BOOL)atomically;
//14.將data寫(xiě)入指定文件逼蒙,并且有寫(xiě)入過(guò)程的設(shè)置條件,帶錯(cuò)誤信息
- (BOOL)writeToFile:(NSString*)path options:(NSDataWritingOptions)writeOptionsMask error:(NSError**)errorPtr;
//15.將data寫(xiě)入指定的url寄疏,并且有寫(xiě)入過(guò)程的設(shè)置條件是牢,帶錯(cuò)誤信息
- (BOOL)writeToURL:(NSURL*)url options:(NSDataWritingOptions)writeOptionsMask error:(NSError**)errorPtr;
//16.搜索在data1內(nèi)部存在data2的位置,并且有搜索配置條件陕截,并且可加入搜索的范圍
- (NSRange)rangeOfData:(NSData*)dataToFind options:(NSDataSearchOptions)mask range:(NSRange)searchRangeNS_AVAILABLE(10_6,4_0);
//17.列舉遍歷字典的方法
- (void) enumerateByteRangesUsingBlock:(void(^)(constvoid*bytes,NSRangebyteRange,BOOL*stop))blockNS_AVAILABLE(10_9,7_0);
@end
/***創(chuàng)建data的所有相關(guān)方法作為一個(gè)類(lèi)別幾種到一塊***/
@interfaceNSData (NSDataCreation)
//18.data的快速創(chuàng)建辦法
+ (instancetype)data;
//19.賦值以bytes開(kāi)頭驳棱,長(zhǎng)度為length的數(shù)據(jù),進(jìn)行初始化使其成為數(shù)據(jù)對(duì)象的內(nèi)容
+ (instancetype)dataWithBytes:(constvoid*)bytes length:(NSUInteger)length;
//20.這個(gè)方法生成的data中保存的是指向數(shù)據(jù)的指針农曲,并沒(méi)有對(duì)數(shù)據(jù)進(jìn)行復(fù)制操作社搅。
+ (instancetype)dataWithBytesNoCopy:(void*)bytes length:(NSUInteger)length;
//21.這個(gè)方法生成的data中保存的是指向數(shù)據(jù)的指針,并沒(méi)有對(duì)數(shù)據(jù)進(jìn)行復(fù)制操作乳规。當(dāng)flag為yes的時(shí)候形葬,生成的data對(duì)象是bytes的所有者,當(dāng)data對(duì)象被釋放的時(shí)候也會(huì)同時(shí)釋放bytes暮的,所以bytes必須是通過(guò)malloc在堆上分配的內(nèi)存笙以。當(dāng)flag為no的時(shí)候,bytes不會(huì)被自動(dòng)釋放冻辩,釋放bytes時(shí)要注意時(shí)機(jī)猖腕,不要再data對(duì)象還被使用的時(shí)候釋放bytes拆祈。
+ (instancetype)dataWithBytesNoCopy:(void*)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
//22.加載file文件,并且有可選擇讀取文件的選項(xiàng)配置條件倘感,帶有錯(cuò)誤信息
+ (instancetype)dataWithContentsOfFile:(NSString*)path options:(NSDataReadingOptions)readOptionsMask error:(NSError**)errorPtr;
//23.加載url缘屹,并且有可選擇讀取文件的選項(xiàng)配置條件,帶有錯(cuò)誤信息
+ (instancetype)dataWithContentsOfURL:(NSURL*)url options:(NSDataReadingOptions)readOptionsMask error:(NSError**)errorPtr;
//24.從參數(shù)path指定的文件讀入二進(jìn)制數(shù)據(jù)侠仇,用該數(shù)據(jù)初始化NSData對(duì)象轻姿。
+ (instancetype)dataWithContentsOfFile:(NSString*)path;
//25.從參數(shù)path指定的url讀入,用該數(shù)據(jù)初始化NSData對(duì)象逻炊。
+ (instancetype)dataWithContentsOfURL:(NSURL*)url;
//26.賦值以bytes開(kāi)頭互亮,長(zhǎng)度為length的數(shù)據(jù),進(jìn)行初始化使其成為數(shù)據(jù)對(duì)象的內(nèi)容
- (instancetype)initWithBytes:(constvoid*)bytes length:(NSUInteger)length;
//27.這個(gè)方法生成的data中保存的是指向數(shù)據(jù)的指針余素,并沒(méi)有對(duì)數(shù)據(jù)進(jìn)行復(fù)制操作豹休。
- (instancetype)initWithBytesNoCopy:(void*)bytes length:(NSUInteger)length;
//28.這個(gè)方法生成的data中保存的是指向數(shù)據(jù)的指針,并沒(méi)有對(duì)數(shù)據(jù)進(jìn)行復(fù)制操作桨吊。當(dāng)flag為yes的時(shí)候威根,生成的data對(duì)象是bytes的所有者,當(dāng)data對(duì)象被釋放的時(shí)候也會(huì)同時(shí)釋放bytes视乐,所以bytes必須是通過(guò)malloc在堆上分配的內(nèi)存洛搀。當(dāng)flag為no的時(shí)候,bytes不會(huì)被自動(dòng)釋放佑淀,釋放bytes時(shí)要注意時(shí)機(jī)留美,不要再data對(duì)象還被使用的時(shí)候釋放bytes。
- (instancetype)initWithBytesNoCopy:(void*)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
//29.
- (instancetype)initWithBytesNoCopy:(void*)bytes length:(NSUInteger)length deallocator:(void(^)(void*bytes,NSUIntegerlength))deallocatorNS_AVAILABLE(10_9,7_0);
//30.從參數(shù)path指定的文件讀入二進(jìn)制數(shù)據(jù)伸刃,用該數(shù)據(jù)初始化NSData對(duì)象谎砾。如果讀取文件失敗,則釋放調(diào)用者并返回nil捧颅,同時(shí)把錯(cuò)誤信息寫(xiě)入指針errorPtr景图。mask是一個(gè)選項(xiàng)信息,用于指定是否使用虛擬內(nèi)存等碉哑。
- (instancetype)initWithContentsOfFile:(NSString*)path options:(NSDataReadingOptions)readOptionsMask error:(NSError**)errorPtr;
//31.從參數(shù)url指定的路徑中讀入挚币,用該數(shù)據(jù)初始化NSData對(duì)象。如果讀取文件失敗谭梗,則釋放調(diào)用者并返回nil忘晤,同時(shí)把錯(cuò)誤信息寫(xiě)入指針errorPtr。mask是一個(gè)選項(xiàng)信息激捏,用于指定是否使用虛擬內(nèi)存等设塔。
- (instancetype)initWithContentsOfURL:(NSURL*)url options:(NSDataReadingOptions)readOptionsMask error:(NSError**)errorPtr;
//32.加載flie文件路徑
- (instancetype)initWithContentsOfFile:(NSString*)path;
//33.加載url
- (instancetype)initWithContentsOfURL:(NSURL*)url;
//34.用指定的NSData對(duì)象aData來(lái)創(chuàng)建一個(gè)新的NSData對(duì)象,參數(shù)可以是NSMutableData對(duì)象。
- (instancetype)initWithData:(NSData*)data;
//35.用指定的NSData對(duì)象aData來(lái)創(chuàng)建一個(gè)新的NSData對(duì)象闰蛔,參數(shù)可以是NSMutableData對(duì)象痕钢。
+ (instancetype)dataWithData:(NSData*)data;
@end
/*** base64方案編碼分類(lèi)? iOS7之后蘋(píng)果給出以下四個(gè)API供實(shí)現(xiàn)base64方案 頭兩個(gè)是處理字符串的,后兩個(gè)是處理UTF-8編碼數(shù)據(jù)的序六,這兩個(gè)承兌的方法功能是一樣的任连,但是有時(shí)候用其中一個(gè)比另一個(gè)效率要高。如果你像要bae64編碼字符串然后寫(xiě)進(jìn)文件例诀,你應(yīng)該使用UTF-8編碼數(shù)據(jù)的這對(duì)方法随抠。如果你打算base64編碼字符串之后用作json,你應(yīng)該使用另外一對(duì)方法編碼解碼一一對(duì)應(yīng)***/
@interfaceNSData (NSDataBase64Encoding)
//36.解碼繁涂。將已經(jīng)base64編碼之后的字符串?dāng)?shù)據(jù)再轉(zhuǎn)化為NSData數(shù)據(jù)拱她,
- (instancetype)initWithBase64EncodedString:(NSString*)base64String options:(NSDataBase64DecodingOptions)optionsNS_AVAILABLE(10_9,7_0);
//37.編碼。將一個(gè)data數(shù)據(jù)利用base64方案轉(zhuǎn)化成base64之后的NSString字符串
- (NSString*)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)optionsNS_AVAILABLE(10_9,7_0);
//38.解碼扔罪。將已經(jīng)base64編碼之后的NSData數(shù)據(jù)再轉(zhuǎn)化為NSData數(shù)據(jù)
- (instancetype)initWithBase64EncodedData:(NSData*)base64Data options:(NSDataBase64DecodingOptions)optionsNS_AVAILABLE(10_9,7_0);
//39.編碼.將一個(gè)data數(shù)據(jù)利用base64方案轉(zhuǎn)化成base64之后的NSData數(shù)據(jù)
- (NSData*)base64EncodedDataWithOptions:(NSDataBase64EncodingOptions)optionsNS_AVAILABLE(10_9,7_0);
示例程序:使用第一對(duì)編碼解碼API秉沼。
NSString * stringExample = [NSString stringWithFormat:@"I Love You"];
NSData * data1 = [stringExample dataUsingEncoding:NSUTF8StringEncoding];
NSString * base64String ? = [data1 base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
NSLog(@"Base64-encoded string is %@",base64String);
//log(Base64-encoded string is SSBMb3ZlIFlvdQ==);
NSData * dataFromString = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSLog(@"string is %@",[NSString stringWithUTF8String:[dataFromString bytes]]);
//log(string is I Love You);
@end
/****************Mutable Data可變data****************/
@interfaceNSMutableData :NSData
//40.可變data的可變字節(jié)
@property(readonly)void*mutableBytesNS_RETURNS_INNER_POINTER;
//41.可變data的長(zhǎng)度
@propertyNSUIntegerlength;
@end
@interfaceNSMutableData (NSExtendedMutableData)
//42.給可變data追加字節(jié)
- (void)appendBytes:(constvoid*)bytes length:(NSUInteger)length;
//43.給data追加其他的data
- (void)appendData:(NSData*)other;
//44.為已經(jīng)存在的data追加新的長(zhǎng)度
- (void)increaseLengthBy:(NSUInteger)extraLength;
示例程序:
NSMutableData * data = [NSMutableData data];
[data increaseLengthBy:5];
NSLog(@"dataLength———%lu",(unsigned long)data.length);
//45.給data替換對(duì)應(yīng)位置的字節(jié)
- (void)replaceBytesInRange:(NSRange)range withBytes:(constvoid*)bytes;
//46.重置可變data對(duì)應(yīng)位置的字節(jié)
- (void)resetBytesInRange:(NSRange)range;
//47.為可變data設(shè)置data數(shù)據(jù)
- (void)setData:(NSData*)data;
//48.給data替換對(duì)應(yīng)位置的字節(jié), 并且存在替換長(zhǎng)度
- (void)replaceBytesInRange:(NSRange)range withBytes:(constvoid*)replacementBytes length:(NSUInteger)replacementLength;
@end0
@interfaceNSMutableData (NSMutableDataCreation)
//49.動(dòng)態(tài)創(chuàng)建可變data并且初始化指定大小
+ (instancetype)dataWithCapacity:(NSUInteger)aNumItems;
//50.動(dòng)態(tài)創(chuàng)建可變data并且初始化指定長(zhǎng)度
+ (instancetype)dataWithLength:(NSUInteger)length;
//51.靜態(tài)創(chuàng)建可變data并且初始化指定大小
- (instancetype)initWithCapacity:(NSUInteger)capacity;
//52.靜態(tài)創(chuàng)建可變data并且初始化指定長(zhǎng)度
- (instancetype)initWithLength:(NSUInteger)length;
@end
//1.data讀取過(guò)程的可選配置條件
typedefNS_OPTIONS(NSUInteger, NSDataReadingOptions) {
NSDataReadingMappedIfSafe =1UL <<0,NSDataReadingUncached =1UL <<1,
NSDataReadingMappedAlwaysNS_ENUM_AVAILABLE(10_7,5_0) =1UL <<3,
NSDataReadingMapped =NSDataReadingMappedIfSafe,// Deprecated name for NSDataReadingMappedIfSafe
NSMappedRead =NSDataReadingMapped,// Deprecated name for NSDataReadingMapped
NSUncachedRead =NSDataReadingUncached// Deprecated name for NSDataReadingUncached
};
//2.data寫(xiě)入過(guò)程的可選配置條件
typedefNS_OPTIONS(NSUInteger, NSDataWritingOptions) {
NSDataWritingAtomic =1UL <<0,
NSDataWritingWithoutOverwritingNS_ENUM_AVAILABLE(10_8,6_0) =1UL <<1,
NSDataWritingFileProtectionNoneNS_ENUM_AVAILABLE_IOS(4_0)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? =0x10000000,
NSDataWritingFileProtectionCompleteNS_ENUM_AVAILABLE_IOS(4_0)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? =0x20000000,
NSDataWritingFileProtectionCompleteUnlessOpenNS_ENUM_AVAILABLE_IOS(5_0)? ? ? ? ? ? ? ? ? ? =0x30000000,
NSDataWritingFileProtectionCompleteUntilFirstUserAuthenticationNS_ENUM_AVAILABLE_IOS(5_0)? =0x40000000,
NSDataWritingFileProtectionMaskNS_ENUM_AVAILABLE_IOS(4_0)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? =0xf0000000,
NSAtomicWrite =NSDataWritingAtomic
};
/****************Data Search Options****************/
//3.data在搜索過(guò)程中的可選配置條件
typedefNS_OPTIONS(NSUInteger, NSDataSearchOptions) {
NSDataSearchBackwards =1UL <<0,
NSDataSearchAnchored =1UL <<1
}NS_ENUM_AVAILABLE(10_6,4_0);
/***下方為已廢棄代碼矿酵,不多做解釋***/
@interfaceNSData (NSDeprecated)
- (void)getBytes:(void*)bufferNS_DEPRECATED(10_0,10_10,2_0,8_0,"This method is unsafe because it could potentially cause buffer overruns. Use -getBytes:length: instead.");
+ (id)dataWithContentsOfMappedFile:(NSString*)pathNS_DEPRECATED(10_0,10_10,2_0,8_0,"Use +dataWithContentsOfURL:options:error: and NSDataReadingMappedIfSafe or NSDataReadingMappedAlways instead.");
- (id)initWithContentsOfMappedFile:(NSString*)pathNS_DEPRECATED(10_0,10_10,2_0,8_0,"Use -initWithContentsOfURL:options:error: and NSDataReadingMappedIfSafe or NSDataReadingMappedAlways instead.");
- (id)initWithBase64Encoding:(NSString*)base64StringNS_DEPRECATED(10_6,10_9,4_0,7_0);
- (NSString*)base64EncodingNS_DEPRECATED(10_6,10_9,4_0,7_0);
@end
/****************Purgeable Data****************/
NS_CLASS_AVAILABLE(10_6,4_0)
@interfaceNSPurgeableData :NSMutableData {
@private
NSUInteger_length;
int32_t_accessCount;
uint8_t_private[32];
void*_reserved;
}
@end