版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.03.30 |
前言
iOS圈內(nèi)有幾個人大家基本都知道彻秆,比如說王巍砰识、唐巧柱搜,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - ibireme等脂信。這里有一篇唐巧對他的專訪癣蟋,還有他的 GitHub - Yaoyuan 和 博客透硝,這里貼出來框架YYKit 框架。接下來幾篇我們就一起來看一下這個框架疯搅。感興趣的可以看上面寫的幾篇濒生。
1. YYKit源碼探究(一) —— 基本概覽
2. YYKit源碼探究(二) —— NSString分類之Hash(一)
3. YYKit源碼探究(三) —— NSString分類之Encode and decode(二)
4. YYKit源碼探究(四) —— NSString分類之Drawing(三)
5. YYKit源碼探究(五) —— NSString分類之Regular Expression(四)
6. YYKit源碼探究(六) —— NSString分類之NSNumber Compatible(五)
7. YYKit源碼探究(七) —— NSString分類之Utilities(六)
8. YYKit源碼探究(八) —— NSNumber分類(一)
9. YYKit源碼探究(九) —— UIFont分類之架構(gòu)分析和Font Traits(一)
10. YYKit源碼探究(十) —— UIFont分類之Create font(二)
11. YYKit源碼探究(十一) —— UIFont分類之Load and unload font(三)
12. YYKit源碼探究(十二) —— UIFont分類之Dump font data(四)
13. YYKit源碼探究(十三) —— UIImage分類之框架結(jié)構(gòu)和Create image部分(一)
14. YYKit源碼探究(十四) —— UIImage分類之Image Info(二)
15. YYKit源碼探究(十五) —— UIImage分類之Modify Image(三)
16. YYKit源碼探究(十六) —— UIImage分類之Image Effect(四)
17. YYKit源碼探究(十七) —— UIImageView分類之架構(gòu)和image部分(一)
18. YYKit源碼探究(十八) —— UIImageView分類之highlight image部分(二)
19. YYKit源碼探究(十九) —— UIScreen分類(一)
20. YYKit源碼探究(二十) —— UIScrollView分類(一)
21. YYKit源碼探究(二十一) —— UITableView分類(一)
22. YYKit源碼探究(二十二) —— UITextField分類(一)
23. YYKit源碼探究(二十三) —— UIView分類(一)
24. YYKit源碼探究(二十四) —— UIPasteboard分類(一)
25. YYKit源碼探究(二十五) —— UIGestureRecognizer分類(一)
26. YYKit源碼探究(二十六) —— UIDevice分類框架及Device Information(一)
27. YYKit源碼探究(二十七) —— UIDevice分類之Network Information(二)
28. YYKit源碼探究(二十八) —— UIDevice分類之Disk Space(三)
29. YYKit源碼探究(二十九) —— UIDevice分類之Memory Information(四)
30. YYKit源碼探究(三十) —— UIDevice分類之CPU Information(五)
31. YYKit源碼探究(三十一) —— UIControl分類(一)
32. YYKit源碼探究(三十二) —— UIColor分類之Create a UIColor Object(一)
33. YYKit源碼探究(三十三) —— UIColor分類之Get color's description(二)
34. YYKit源碼探究(三十四) —— UIColor分類之Retrieving Color Information(三)
35. YYKit源碼探究(三十五) —— UIButton分類之image(一)
36. YYKit源碼探究(三十六) —— UIButton分類之background image(二)
37. YYKit源碼探究(三十七) —— UIBezierPath分類(一)
38. YYKit源碼探究(三十八) —— UIBarButtonItem分類(一)
39. YYKit源碼探究(三十九) —— UIApplication分類(一)
40. YYKit源碼探究(四十) —— NSTimer分類(一)
41. YYKit源碼探究(四十一) —— NSParagraphStyle分類(一)
42. YYKit源碼探究(四十二) —— NSObject分類之YYModel(一)
43. YYKit源碼探究(四十三) —— NSObject分類之KVO(二)
44. YYKit源碼探究(四十四) —— NSObject分類之Sending messages with variable parameters(三)
45. YYKit源碼探究(四十五) —— NSObject分類之Swap method (Swizzling)(四)
46. YYKit源碼探究(四十六) —— NSObject分類之Associate value(五)
47. YYKit源碼探究(四十七) —— NSObject分類之Other(六)
48. YYKit源碼探究(四十八) —— NSNotificationCenter分類(一)
49. YYKit源碼探究(四十九) —— NSKeyedUnarchiver分類(一)
回顧
上一篇主要介紹了NSKeyedUnarchiver
分類,這一篇主要看一下NSDictionary
分類幔欧。
框架結(jié)構(gòu)
下面我們看一下框架結(jié)構(gòu)罪治。
API - Dictionary Convertor
下面我們就一起看一下API文檔。
/**
Creates and returns a dictionary from a specified property list data.
@param plist A property list data whose root object is a dictionary.
@return A new dictionary created from the binary plist data, or nil if an error occurs.
*/
+ (nullable NSDictionary *)dictionaryWithPlistData:(NSData *)plist;
/**
Creates and returns a dictionary from a specified property list xml string.
@param plist A property list xml string whose root object is a dictionary.
@return A new dictionary created from the plist string, or nil if an error occurs.
@discussion Apple has implemented this method, but did not make it public.
*/
+ (nullable NSDictionary *)dictionaryWithPlistString:(NSString *)plist;
/**
Serialize the dictionary to a binary property list data.
@return A binary plist data, or nil if an error occurs.
@discussion Apple has implemented this method, but did not make it public.
*/
- (nullable NSData *)plistData;
/**
Serialize the dictionary to a xml property list string.
@return A plist xml string, or nil if an error occurs.
*/
- (nullable NSString *)plistString;
/**
Returns a new array containing the dictionary's keys sorted.
The keys should be NSString, and they will be sorted ascending.
@return A new array containing the dictionary's keys,
or an empty array if the dictionary has no entries.
*/
- (NSArray *)allKeysSorted;
/**
Returns a new array containing the dictionary's values sorted by keys.
The order of the values in the array is defined by keys.
The keys should be NSString, and they will be sorted ascending.
@return A new array containing the dictionary's values sorted by keys,
or an empty array if the dictionary has no entries.
*/
- (NSArray *)allValuesSortedByKeys;
/**
Returns a BOOL value tells if the dictionary has an object for key.
@param key The key.
*/
- (BOOL)containsObjectForKey:(id)key;
/**
Returns a new dictionary containing the entries for keys.
If the keys is empty or nil, it just returns an empty dictionary.
@param keys The keys.
@return The entries for the keys.
*/
- (NSDictionary *)entriesForKeys:(NSArray *)keys;
/**
Convert dictionary to json string. return nil if an error occurs.
*/
- (nullable NSString *)jsonStringEncoded;
/**
Convert dictionary to json string formatted. return nil if an error occurs.
*/
- (nullable NSString *)jsonPrettyStringEncoded;
/**
Try to parse an XML and wrap it into a dictionary.
If you just want to get some value from a small xml, try this.
example XML: "<config><a href="test.com">link</a></config>"
example Return: @{@"_name":@"config", @"a":{@"_text":@"link",@"href":@"test.com"}}
@param xmlDataOrString XML in NSData or NSString format.
@return Return a new dictionary, or nil if an error occurs.
*/
+ (nullable NSDictionary *)dictionaryWithXML:(id)xmlDataOrString;
下面我們就詳細的看一下這個API
1. + (nullable NSDictionary *)dictionaryWithPlistData:(NSData *)plist;
該方法的作用就是將指定的plist數(shù)據(jù)轉(zhuǎn)化為一個字典礁蔗。
方法實現(xiàn)
+ (NSDictionary *)dictionaryWithPlistData:(NSData *)plist {
if (!plist) return nil;
NSDictionary *dictionary = [NSPropertyListSerialization propertyListWithData:plist options:NSPropertyListImmutable format:NULL error:NULL];
if ([dictionary isKindOfClass:[NSDictionary class]]) return dictionary;
return nil;
}
2. + (nullable NSDictionary *)dictionaryWithPlistString:(NSString *)plist;
該方法的作用就是將指定的property list xml
字符串轉(zhuǎn)化為一個字典觉义。
方法實現(xiàn)
+ (NSDictionary *)dictionaryWithPlistString:(NSString *)plist {
if (!plist) return nil;
NSData *data = [plist dataUsingEncoding:NSUTF8StringEncoding];
return [self dictionaryWithPlistData:data];
}
3. - (nullable NSData *)plistData;
該方法的作用就是將字典序列化一個binary property list data
。
方法實現(xiàn)
- (NSData *)plistData {
return [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListBinaryFormat_v1_0 options:kNilOptions error:NULL];
}
4. - (nullable NSString *)plistString;
該方法的作用就是將字典序列化一個xml property list
字符串浴井。
方法實現(xiàn)
- (NSString *)plistString {
NSData *xmlData = [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListXMLFormat_v1_0 options:kNilOptions error:NULL];
if (xmlData) return xmlData.utf8String;
return nil;
}
5. - (NSArray *)allKeysSorted;
該方法的作用就是將字典的所有key查出來晒骇,key必須是NSString類型的,并且在數(shù)組中按照升序進行排列磺浙。
方法實現(xiàn)
- (NSArray *)allKeysSorted {
return [[self allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}
6. - (NSArray *)allValuesSortedByKeys;
該方法的作用就是返回key對應(yīng)的值洪囤,并且數(shù)組中的值是按照升序排列的。
方法實現(xiàn)
- (NSArray *)allValuesSortedByKeys {
NSArray *sortedKeys = [self allKeysSorted];
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (id key in sortedKeys) {
[arr addObject:self[key]];
}
return [arr copy];
}
7. - (BOOL)containsObjectForKey:(id)key;
該方法的作用就是判斷字典是否有指定key對應(yīng)的value撕氧。
方法實現(xiàn)
- (BOOL)containsObjectForKey:(id)key {
if (!key) return NO;
return self[key] != nil;
}
8. - (NSDictionary *)entriesForKeys:(NSArray *)keys;
該方法的作用就是返回keys對應(yīng)的新的字典瘤缩。
方法實現(xiàn)
- (NSDictionary *)entriesForKeys:(NSArray *)keys {
NSMutableDictionary *dic = [NSMutableDictionary new];
for (id key in keys) {
id value = self[key];
if (value) dic[key] = value;
}
return [dic copy];
}
9. - (nullable NSString *)jsonStringEncoded;
該方法的作用就是將字典轉(zhuǎn)化為json字符串。
方法實現(xiàn)
- (NSString *)jsonStringEncoded {
if ([NSJSONSerialization isValidJSONObject:self]) {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:0 error:&error];
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
if (!error) return json;
}
return nil;
}
10. - (nullable NSString *)jsonPrettyStringEncoded;
該方法的作用就是將字典轉(zhuǎn)化為json字符串格式伦泥。
方法實現(xiàn)
- (NSString *)jsonPrettyStringEncoded {
if ([NSJSONSerialization isValidJSONObject:self]) {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
if (!error) return json;
}
return nil;
}
11. + (nullable NSDictionary *)dictionaryWithXML:(id)xmlDataOrString;
嘗試解析XML并將其包裝到字典中剥啤。 如果你只是想從一個小的XML獲得一些價值,試試這個方法不脯。
示例展示
example XML: "<config><a href="test.com">link</a></config>"
example Return: @{@"_name":@"config", @"a":{@"_text":@"link",@"href":@"test.com"}}
方法實現(xiàn)
+ (NSDictionary *)dictionaryWithXML:(id)xml {
_YYXMLDictionaryParser *parser = nil;
if ([xml isKindOfClass:[NSString class]]) {
parser = [[_YYXMLDictionaryParser alloc] initWithString:xml];
} else if ([xml isKindOfClass:[NSData class]]) {
parser = [[_YYXMLDictionaryParser alloc] initWithData:xml];
}
return [parser result];
}
后記
本篇主要解析了
Dictionary Convertor
部分府怯,感興趣的給個贊或者關(guān)注~~~~