版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2018.03.31 |
前言
iOS圈內(nèi)有幾個(gè)人大家基本都知道函卒,比如說(shuō)王巍适瓦、唐巧驯遇,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - ibireme等土全。這里有一篇唐巧對(duì)他的專訪捎琐,還有他的 GitHub - Yaoyuan 和 博客,這里貼出來(lái)框架YYKit 框架裹匙。接下來(lái)幾篇我們就一起來(lái)看一下這個(gè)框架瑞凑。感興趣的可以看上面寫(xiě)的幾篇。
1. YYKit源碼探究(一) —— 基本概覽
2. YYKit源碼探究(二) —— NSString分類(lèi)之Hash(一)
3. YYKit源碼探究(三) —— NSString分類(lèi)之Encode and decode(二)
4. YYKit源碼探究(四) —— NSString分類(lèi)之Drawing(三)
5. YYKit源碼探究(五) —— NSString分類(lèi)之Regular Expression(四)
6. YYKit源碼探究(六) —— NSString分類(lèi)之NSNumber Compatible(五)
7. YYKit源碼探究(七) —— NSString分類(lèi)之Utilities(六)
8. YYKit源碼探究(八) —— NSNumber分類(lèi)(一)
9. YYKit源碼探究(九) —— UIFont分類(lèi)之架構(gòu)分析和Font Traits(一)
10. YYKit源碼探究(十) —— UIFont分類(lèi)之Create font(二)
11. YYKit源碼探究(十一) —— UIFont分類(lèi)之Load and unload font(三)
12. YYKit源碼探究(十二) —— UIFont分類(lèi)之Dump font data(四)
13. YYKit源碼探究(十三) —— UIImage分類(lèi)之框架結(jié)構(gòu)和Create image部分(一)
14. YYKit源碼探究(十四) —— UIImage分類(lèi)之Image Info(二)
15. YYKit源碼探究(十五) —— UIImage分類(lèi)之Modify Image(三)
16. YYKit源碼探究(十六) —— UIImage分類(lèi)之Image Effect(四)
17. YYKit源碼探究(十七) —— UIImageView分類(lèi)之架構(gòu)和image部分(一)
18. YYKit源碼探究(十八) —— UIImageView分類(lèi)之highlight image部分(二)
19. YYKit源碼探究(十九) —— UIScreen分類(lèi)(一)
20. YYKit源碼探究(二十) —— UIScrollView分類(lèi)(一)
21. YYKit源碼探究(二十一) —— UITableView分類(lèi)(一)
22. YYKit源碼探究(二十二) —— UITextField分類(lèi)(一)
23. YYKit源碼探究(二十三) —— UIView分類(lèi)(一)
24. YYKit源碼探究(二十四) —— UIPasteboard分類(lèi)(一)
25. YYKit源碼探究(二十五) —— UIGestureRecognizer分類(lèi)(一)
26. YYKit源碼探究(二十六) —— UIDevice分類(lèi)框架及Device Information(一)
27. YYKit源碼探究(二十七) —— UIDevice分類(lèi)之Network Information(二)
28. YYKit源碼探究(二十八) —— UIDevice分類(lèi)之Disk Space(三)
29. YYKit源碼探究(二十九) —— UIDevice分類(lèi)之Memory Information(四)
30. YYKit源碼探究(三十) —— UIDevice分類(lèi)之CPU Information(五)
31. YYKit源碼探究(三十一) —— UIControl分類(lèi)(一)
32. YYKit源碼探究(三十二) —— UIColor分類(lèi)之Create a UIColor Object(一)
33. YYKit源碼探究(三十三) —— UIColor分類(lèi)之Get color's description(二)
34. YYKit源碼探究(三十四) —— UIColor分類(lèi)之Retrieving Color Information(三)
35. YYKit源碼探究(三十五) —— UIButton分類(lèi)之image(一)
36. YYKit源碼探究(三十六) —— UIButton分類(lèi)之background image(二)
37. YYKit源碼探究(三十七) —— UIBezierPath分類(lèi)(一)
38. YYKit源碼探究(三十八) —— UIBarButtonItem分類(lèi)(一)
39. YYKit源碼探究(三十九) —— UIApplication分類(lèi)(一)
40. YYKit源碼探究(四十) —— NSTimer分類(lèi)(一)
41. YYKit源碼探究(四十一) —— NSParagraphStyle分類(lèi)(一)
42. YYKit源碼探究(四十二) —— NSObject分類(lèi)之YYModel(一)
43. YYKit源碼探究(四十三) —— NSObject分類(lèi)之KVO(二)
44. YYKit源碼探究(四十四) —— NSObject分類(lèi)之Sending messages with variable parameters(三)
45. YYKit源碼探究(四十五) —— NSObject分類(lèi)之Swap method (Swizzling)(四)
46. YYKit源碼探究(四十六) —— NSObject分類(lèi)之Associate value(五)
47. YYKit源碼探究(四十七) —— NSObject分類(lèi)之Other(六)
48. YYKit源碼探究(四十八) —— NSNotificationCenter分類(lèi)(一)
49. YYKit源碼探究(四十九) —— NSKeyedUnarchiver分類(lèi)(一)
50. YYKit源碼探究(五十) —— NSDictionary分類(lèi)之Dictionary Convertor(一)
51. YYKit源碼探究(五十一) —— NSDictionary分類(lèi)之Dictionary Value Getter(二)
52. YYKit源碼探究(五十二) —— NSDictionary分類(lèi)之NSMutableDictionary(三)
53. YYKit源碼探究(五十三) —— NSDate分類(lèi)之Component Properties(一)
54. YYKit源碼探究(五十四) —— NSDate分類(lèi)之Date modify(二)
55. YYKit源碼探究(五十五) —— NSDate分類(lèi)之Date Format(三)
56. YYKit源碼探究(五十六) —— NSData分類(lèi)之Hash(一)
57. YYKit源碼探究(五十七) —— NSData分類(lèi)之Encrypt and Decrypt(二)
回顧
上一篇主要介紹了NSData的Encrypt and Decrypt
分類(lèi)部分概页,這一篇主要看一下NSData
分類(lèi)的Encode and decode
部分籽御。
API
下面我們就看一下API
/**
Returns string decoded in UTF8.
*/
- (nullable NSString *)utf8String;
/**
Returns a uppercase NSString in HEX.
*/
- (nullable NSString *)hexString;
/**
Returns an NSData from hex string.
@param hexString The hex string which is case insensitive.
@return a new NSData, or nil if an error occurs.
*/
+ (nullable NSData *)dataWithHexString:(NSString *)hexString;
/**
Returns an NSString for base64 encoded.
*/
- (nullable NSString *)base64EncodedString;
/**
Returns an NSData from base64 encoded string.
@warning This method has been implemented in iOS7.
@param base64EncodedString The encoded string.
*/
+ (nullable NSData *)dataWithBase64EncodedString:(NSString *)base64EncodedString;
/**
Returns an NSDictionary or NSArray for decoded self.
Returns nil if an error occurs.
*/
- (nullable id)jsonValueDecoded;
下面我們就詳細(xì)的看一下這個(gè)API
1. - (nullable NSString *)utf8String;
該方法的作用就是返回UTF8解碼的字符串。
方法實(shí)現(xiàn)
- (NSString *)utf8String {
if (self.length > 0) {
return [[NSString alloc] initWithData:self encoding:NSUTF8StringEncoding];
}
return @"";
}
2. - (nullable NSString *)hexString;
該方法的作用就是返回大寫(xiě)的十六進(jìn)制的字符串。
方法實(shí)現(xiàn)
- (NSString *)hexString {
NSUInteger length = self.length;
NSMutableString *result = [NSMutableString stringWithCapacity:length * 2];
const unsigned char *byte = self.bytes;
for (int i = 0; i < length; i++, byte++) {
[result appendFormat:@"%02X", *byte];
}
return result;
}
3. + (nullable NSData *)dataWithHexString:(NSString *)hexString;
該方法的作用就是根據(jù)十六進(jìn)制的字符串返回NSData數(shù)據(jù)篱蝇。
方法實(shí)現(xiàn)
+ (NSData *)dataWithHexString:(NSString *)hexStr {
hexStr = [hexStr stringByReplacingOccurrencesOfString:@" " withString:@""];
hexStr = [hexStr lowercaseString];
NSUInteger len = hexStr.length;
if (!len) return nil;
unichar *buf = malloc(sizeof(unichar) * len);
if (!buf) return nil;
[hexStr getCharacters:buf range:NSMakeRange(0, len)];
NSMutableData *result = [NSMutableData data];
unsigned char bytes;
char str[3] = { '\0', '\0', '\0' };
int i;
for (i = 0; i < len / 2; i++) {
str[0] = buf[i * 2];
str[1] = buf[i * 2 + 1];
bytes = strtol(str, NULL, 16);
[result appendBytes:&bytes length:1];
}
free(buf);
return result;
}
4. - (nullable NSString *)base64EncodedString;
該方法的作用就是返回base64加密的字符串?dāng)?shù)據(jù)贺待。
方法實(shí)現(xiàn)
- (NSString *)base64EncodedString {
NSUInteger length = self.length;
if (length == 0)
return @"";
NSUInteger out_length = ((length + 2) / 3) * 4;
uint8_t *output = malloc(((out_length + 2) / 3) * 4);
if (output == NULL)
return nil;
const char *input = self.bytes;
NSInteger i, value;
for (i = 0; i < length; i += 3) {
value = 0;
for (NSInteger j = i; j < i + 3; j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger index = (i / 3) * 4;
output[index + 0] = base64EncodingTable[(value >> 18) & 0x3F];
output[index + 1] = base64EncodingTable[(value >> 12) & 0x3F];
output[index + 2] = ((i + 1) < length)
? base64EncodingTable[(value >> 6) & 0x3F]
: '=';
output[index + 3] = ((i + 2) < length)
? base64EncodingTable[(value >> 0) & 0x3F]
: '=';
}
NSString *base64 = [[NSString alloc] initWithBytes:output
length:out_length
encoding:NSASCIIStringEncoding];
free(output);
return base64;
}
static const char base64EncodingTable[64]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
5. + (nullable NSData *)dataWithBase64EncodedString:(NSString *)base64EncodedString;
該方法的作用就是從base64加密的字符串返回NSData類(lèi)型的數(shù)據(jù)徽曲。
方法實(shí)現(xiàn)
+ (NSData *)dataWithBase64EncodedString:(NSString *)base64EncodedString {
NSInteger length = base64EncodedString.length;
const char *string = [base64EncodedString cStringUsingEncoding:NSASCIIStringEncoding];
if (string == NULL)
return nil;
while (length > 0 && string[length - 1] == '=')
length--;
NSInteger outputLength = length * 3 / 4;
NSMutableData *data = [NSMutableData dataWithLength:outputLength];
if (data == nil)
return nil;
if (length == 0)
return data;
uint8_t *output = data.mutableBytes;
NSInteger inputPoint = 0;
NSInteger outputPoint = 0;
while (inputPoint < length) {
char i0 = string[inputPoint++];
char i1 = string[inputPoint++];
char i2 = inputPoint < length ? string[inputPoint++] : 'A';
char i3 = inputPoint < length ? string[inputPoint++] : 'A';
output[outputPoint++] = (base64DecodingTable[i0] << 2)
| (base64DecodingTable[i1] >> 4);
if (outputPoint < outputLength) {
output[outputPoint++] = ((base64DecodingTable[i1] & 0xf) << 4)
| (base64DecodingTable[i2] >> 2);
}
if (outputPoint < outputLength) {
output[outputPoint++] = ((base64DecodingTable[i2] & 0x3) << 6)
| base64DecodingTable[i3];
}
}
return data;
}
static const short base64DecodingTable[256] = {
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -1, -1, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
-2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
-2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
};
6. - (nullable id)jsonValueDecoded;
該方法的作用就是對(duì)自己進(jìn)行解密并返回NSDictionary
或 NSArray
對(duì)象零截。
方法實(shí)現(xiàn)
- (id)jsonValueDecoded {
NSError *error = nil;
id value = [NSJSONSerialization JSONObjectWithData:self options:kNilOptions error:&error];
if (error) {
NSLog(@"jsonValueDecoded error:%@", error);
}
return value;
}
后記
本篇主要介紹了NSData分類(lèi)的Encode and decode,感興趣的可以給個(gè)贊或者關(guān)注~~~~