/**************** 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è)置選擇
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 <NSCopying, NSMutableCopying, NSSecureCoding>
//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
//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
};
/**************** Data Search Options
****************/
//3.data在搜索過(guò)程中的可選配置條件
typedef NS_OPTIONS(NSUInteger, NSDataSearchOptions) {
NSDataSearchBackwards = 1UL << 0,
NSDataSearchAnchored = 1UL << 1
} NS_ENUM_AVAILABLE(10_6, 4_0);
/*** 下方為已廢棄代碼铆农,不多做解釋 ***/
@interface NSData (NSDeprecated)
- (void)getBytes:(void *)buffer NS_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 *)path NS_DEPRECATED(10_0, 10_10, 2_0, 8_0, "Use +dataWithContentsOfURL:options:error: and NSDataReadingMappedIfSafe or NSDataReadingMappedAlways instead.");
- (id)initWithContentsOfMappedFile:(NSString *)path NS_DEPRECATED(10_0, 10_10, 2_0, 8_0, "Use -initWithContentsOfURL:options:error: and NSDataReadingMappedIfSafe or NSDataReadingMappedAlways instead.");
- (id)initWithBase64Encoding:(NSString *)base64String NS_DEPRECATED(10_6, 10_9, 4_0, 7_0);
- (NSString *)base64Encoding NS_DEPRECATED(10_6, 10_9, 4_0, 7_0);
@end
/**************** Purgeable Data
****************/
NS_CLASS_AVAILABLE(10_6, 4_0)
@interface NSPurgeableData : NSMutableData <NSDiscardableContent> {
@private
NSUInteger _length;
int32_t _accessCount;
uint8_t _private[32];
void *_reserved;
}