學(xué)習(xí)NSData,在網(wǎng)上找資料竟然都是拷貝的純代碼,沒(méi)人去解釋。在這種網(wǎng)上沒(méi)資料的情況下,整理這個(gè)API文件好難,好艱辛,在這貢獻(xiàn)給大家了。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功能 ***//
/1.data讀取過(guò)程的可選配置條件typedef NS_OPTIONS(NSUInteger, NSDataReadingOptions) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NSDataReadingMappedIfSafe =? 1UL << 0, ? ? ? ? ? ? ? ? ?NSDataReadingUncached = 1UL << 1, ? ? ? ? ? ? NSDataReadingMappedAlways NS_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ò)程的可選配置條件typedef NS_OPTIONS(NSUInteger, NSDataWritingOptions) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSDataWritingAtomic = 1UL << 0, ? ? ? ? ? ?NSDataWritingWithoutOverwriting NS_ENUM_AVAILABLE(10_8, 6_0) = 1UL << 1, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSDataWritingFileProtectionNone NS_ENUM_AVAILABLE_IOS(4_0)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0x10000000, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSDataWritingFileProtectionComplete NS_ENUM_AVAILABLE_IOS(4_0)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0x20000000, ? ? ? ? ? ? ? ? ?NSDataWritingFileProtectionCompleteUnlessOpen NS_ENUM_AVAILABLE_IOS(5_0) = 0x30000000, NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication NS_ENUM_AVAILABLE_IOS(5_0)? = 0x40000000,? ? NSDataWritingFileProtectionMask NS_ENUM_AVAILABLE_IOS(4_0)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 0xf0000000, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSAtomicWrite = NSDataWritingAtomic? };
//3.data在搜索過(guò)程中的可選配置條件typedef NS_OPTIONS(NSUInteger, NSDataSearchOptions) {? ? NSDataSearchBackwards = 1UL << 0,? ? NSDataSearchAnchored = 1UL << 1} NS_ENUM_AVAILABLE(10_6, 4_0);
//4.base64編碼中的一些設(shè)置選擇typedef NS_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é)typedef NS_OPTIONS(NSUInteger, NSDataBase64DecodingOptions) {? ? NSDataBase64DecodingIgnoreUnknownCharacters = 1UL << 0 } NS_ENUM_AVAILABLE(10_9, 7_0);/**************** Immutable Data不可變的Data****************/@interface NSData : NSObject
//6.獲取data長(zhǎng)度的屬性
@property (readonly) NSUInteger length;
//7.返回data對(duì)象的首指針
@property (readonly) const void *bytes NS_RETURNS_INNER_POINTER;
@end
@interface NSData (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)searchRange NS_AVAILABLE(10_6, 4_0);
//17.列舉遍歷字典的方法
- (void) enumerateByteRangesUsingBlock:(void (^)(const void *bytes, NSRange byteRange, BOOL *stop))block NS_AVAILABLE(10_9, 7_0);
@end
/*** 創(chuàng)建data的所有相關(guān)方法作為一個(gè)類(lèi)別幾種到一塊 ***/
@interface NSData (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:(const void *)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:(const void *)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, NSUInteger length))deallocator NS_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)***/
@interface NSData (NSDataBase64Encoding)
//36.解碼。將已經(jīng)base64編碼之后的字符串?dāng)?shù)據(jù)再轉(zhuǎn)化為NSData數(shù)據(jù),
- (instancetype)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options NS_AVAILABLE(10_9, 7_0);
//37.編碼。將一個(gè)data數(shù)據(jù)利用base64方案轉(zhuǎn)化成base64之后的NSString字符串
- (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options NS_AVAILABLE(10_9, 7_0);
//38.解碼。將已經(jīng)base64編碼之后的NSData數(shù)據(jù)再轉(zhuǎn)化為NSData數(shù)據(jù)
- (instancetype)initWithBase64EncodedData:(NSData *)base64Data options:(NSDataBase64DecodingOptions)options NS_AVAILABLE(10_9, 7_0);
//39.編碼.將一個(gè)data數(shù)據(jù)利用base64方案轉(zhuǎn)化成base64之后的NSData數(shù)據(jù)
- (NSData *)base64EncodedDataWithOptions:(NSDataBase64EncodingOptions)options NS_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
****************/
@interface NSMutableData : NSData
//40.可變data的可變字節(jié)
@property (readonly) void *mutableBytes NS_RETURNS_INNER_POINTER;
//41.可變data的長(zhǎng)度
@property NSUInteger length;
@end
@interface NSMutableData (NSExtendedMutableData)
//42.給可變data追加字節(jié)
- (void)appendBytes:(const void *)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:(const void *)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:(const void *)replacementBytes length:(NSUInteger)replacementLength;
@end0
@interface NSMutableData (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