第一次學(xué)習(xí)英文官方文檔板熊,有很多不懂得地方框全,暫時(shí)空著,回頭查詢中文文檔再補(bǔ)全邻邮,如果有大神愿意指點(diǎn)竣况,請留言,感謝M惭稀5と!
1鸭蛙、創(chuàng)建一個(gè)dictionary
//1>直接創(chuàng)建一個(gè)空dictionary
+ (instancetype)dictionary;
//demo
NSDictionary *dict = [NSDictionary dictionary];
//2>獲取本地路徑下文件中的dictionary
+ (NSDictionary<KeyType,ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path;
//demo
NSDictionary *dict1 = [NSDictionary dictionaryWithContentsOfFile:path];
//path是一個(gè)絕對路徑或者是相對路徑摹恨,由這個(gè)路徑獲取的文件內(nèi)容必須是一個(gè)字典,否則dict1為nil娶视。
//3>獲取接口返回的dictionary
+ (NSDictionary<KeyType,ObjectType> *)dictionaryWithContentsOfURL:(NSURL *)url;
//demo
NSDictionary *dict1 = [NSDictionary dictionaryWithContentsOfURL:url];
//url是一個(gè)接口晒哄,由這個(gè)接口獲取的文件內(nèi)容必須是一個(gè)字典睁宰,否則dict1為nil。
//4>將一個(gè)dictionary中的key和value復(fù)制到一個(gè)新的dictionary中
+ (instancetype)dictionaryWithDictionary:(NSDictionary<KeyType,ObjectType> *)dict;
//demo
NSDictionary *dict1 = [NSDictionary dictionaryWithDictionary:dict];
//dict:一個(gè)dictionary
//5>創(chuàng)建一個(gè)只有一個(gè)key和value的dictionary
+ (instancetype)dictionaryWithObject:(ObjectType)object forKey:(id<NSCopying>)key;
//demo
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:value forKey:key];
//6>創(chuàng)建一個(gè)有多個(gè)value和key的dictionary
+ (instancetype)dictionaryWithObjects:(NSArray<ObjectType> *)objects forKeys:(NSArray<id<NSCopying>> *)keys;
//demo
NSDictionary *dict1 = [NSDictionary dictionaryWithObjects:@[value1,value2,value3] forKeys:@[key1,key2,key3]];
//value和key的順序必須一一對應(yīng)
//7>G蘖琛F馍怠!=夏尽:旆!7フTず睢!峰锁!沒搞清楚N凇!:缃糜芳!
+ (instancetype)dictionaryWithObjects:(const ObjectType _Nonnull [])objects forKeys:(const id<NSCopying> _Nonnull [])keys count:(NSUInteger)cnt;
//8>創(chuàng)建一個(gè)有多個(gè)value和key的dictionary
+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
//demo
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:value1,key1,value2,key2,value3,key3, nil];
2、初始化dictionary實(shí)例
//1>實(shí)例化一個(gè)空dictionary
- (instancetype)init;
//demo
NSDictionary *dict = [[NSDictionary alloc] init];
//2>實(shí)例化一個(gè)dictionary獲取本地路徑下文件中的數(shù)組
- (NSDictionary<KeyType,ObjectType> *)initWithContentsOfFile:(NSString *)path;
//demo
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
//3>實(shí)例化一個(gè)dictionary接收接口返回的字典
- (NSDictionary<KeyType,ObjectType> *)initWithContentsOfURL:(NSURL *)url;
//demo
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL:url];
//4>實(shí)例化一個(gè)dictionary千诬,并將里一個(gè)數(shù)組中的元素復(fù)制
- (instancetype)initWithDictionary:(NSDictionary<KeyType,ObjectType> *)otherDictionary;
//demo
NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:dict1];
//KD俊!P彀蟆P巴浴!0燎选R惴谩!E陶ァ喻粹!沒搞懂
//5>實(shí)例化一個(gè)dictionary,并將里一個(gè)數(shù)組中的元素復(fù)制(copyItems)
- (instancetype)initWithDictionary:(NSDictionary<KeyType,ObjectType> *)otherDictionary copyItems:(BOOL)flag;
//6>實(shí)例化一個(gè)有多個(gè)value和key的dictionary
- (instancetype)initWithObjects:(NSArray<ObjectType> *)objects forKeys:(NSArray<id<NSCopying>> *)keys;
//demo
NSDictionary *dict = [[NSDictionary alloc]initWithObjects:@[value1,value2,value3] forKeys:@[key1,key2,key3]];
//7>2菅病J匚亍!I胶查乒!沒搞懂
- (instancetype)initWithObjects:(ObjectType _Nonnull const [])objects forKeys:(id<NSCopying> _Nonnull const [])keys count:(NSUInteger)cnt;
//8>實(shí)例化一個(gè)有多個(gè)value和key的dictionary
- (instancetype)initWithObjectsAndKeys:(id)firstObject, ...;
//demo
NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:value1,key1,value2,key2,value3,key3, nil];
3、Creates a shared key set object for the specified keys.S艟埂B昶!E锬丁蓖议!沒搞懂
+ (id)sharedKeySetForKeys:(NSArray<id<NSCopying>> *)keys;
4虏杰、字典中條目的個(gè)數(shù)
@property(readonly, copy) NSArray<KeyType> *allKeys;
//demo
NSUInteger num = dict. count;
5、字典比較
- (BOOL)isEqualToDictionary:(NSDictionary<KeyType,ObjectType> *)otherDictionary;
//demo
BOOL isEqule = [dict1 isEqualToDictionary:dict];
6勒虾、key和value的相關(guān)方法
//1>獲取字典中所有的key值
@property(readonly, copy) NSArray<KeyType> *allKeys;
//demo
NSArray *keyArr = dict.allKeys;
//2>獲取所有值等于value的key的值
- (NSArray<KeyType> *)allKeysForObject:(ObjectType)anObject;
//demo
NSDictionary *dict = @{@"key1":value1,@"key2":value2,@"key3":value1};
NSArray *keyArr = [dict allKeysForObject:value1];
//返回
//(
// key1,
// key3
//)
//3>獲取字典中所有的value值
@property(readonly, copy) NSArray<ObjectType> *allValues;
//demo
NSArray *valueArr = dict.allValues;
//4>Returns by reference C arrays of the keys and values in the dictionary.不懂7睦!4雍场V莸堋!5土恪!拯杠!
- (void)getObjects:(ObjectType _Nonnull [])objects andKeys:(KeyType _Nonnull [])keys;
//5>獲取指定key的value
- (ObjectType)objectForKey:(KeyType)aKey;
//demo
NSString *str = [dict objectForKey:key];
//6>獲取指定key的value(不知道和上一個(gè)方法的具體區(qū)別是啥掏婶,暫時(shí)知道都能獲取value)
- (ObjectType)objectForKeyedSubscript:(KeyType)key;
//demo
NSString *str = [dict objectForKeyedSubscript:key];
//7>獲取指定key的value,如果沒找到潭陪,返回marker
- (NSArray<ObjectType> *)objectsForKeys:(NSArray<KeyType> *)keys notFoundMarker:(ObjectType)marker;
//demo
NSArray *arr = [dict objectsForKeys:@[key] notFoundMarker:maker];
//如果找到對應(yīng)的key值雄妥,則返回對應(yīng)的value數(shù)組,若沒找到依溯,返回只有marker的數(shù)組
//8>獲取指定key的value值(key的類型必須是NSString)
- (ObjectType)valueForKey:(NSString *)key;
//demo
NSString *str = [dict objectForKeyedSubscript:key];
7老厌、枚舉字典
//1>取出字典中的key值,組成枚舉
- (NSEnumerator *)keyEnumerator
//demo
NSEnumerator *keyEnum = [dict1 keyEnumerator];
//2>取出字典中的object黎炉,組成枚舉
- (NSEnumerator<ObjectType> *)objectEnumerator;
//demo
NSEnumerator *objectEnum = [dict1 objectEnumerator];
//3>遍歷字典枝秤,block中處理字典中的數(shù)據(jù)
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(KeyType key, ObjectType obj, BOOL *stop))block;
//demo
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"%@,%@,%d",key,obj,*stop);
}];
//4>遍歷字典,block中處理字典中的數(shù)據(jù)(暫時(shí)不清楚NSEnumerationReverse和NSEnumerationConcurrent的區(qū)別)
- (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(KeyType key, ObjectType obj, BOOL *stop))block;
//demo
[dict enumerateKeysAndObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"%@,%@,%d",key,obj,*stop);
}];
8慷嗜、分類字典(暫時(shí)不懂淀弹,遇見再研究)
//1>
- (NSArray<KeyType> *)keysSortedByValueUsingSelector:(SEL)comparator;
//2>
- (NSArray<KeyType> *)keysSortedByValueUsingComparator:(NSComparator)cmptr;
//3>
- (NSArray<KeyType> *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr;
9、字典過濾(暫時(shí)不懂庆械,遇見再研究)
//1>
- (NSSet<KeyType> *)keysOfEntriesPassingTest:(BOOL (^)(KeyType key, ObjectType obj, BOOL *stop))predicate;
//2>
- (NSSet<KeyType> *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(KeyType key, ObjectType obj, BOOL *stop))predicate;
10薇溃、保存字典進(jìn)一個(gè)文件
//1>
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
//2>
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;
11、訪問文件屬性
//1>
- (NSDate *)fileCreationDate;
//2>
- (BOOL)fileExtensionHidden;
//3>
- (NSNumber *)fileGroupOwnerAccountID;
//4>
- (NSString *)fileGroupOwnerAccountName;
//5>
- (OSType)fileHFSCreatorCode;
//6>
- (OSType)fileHFSTypeCode;
//7>
- (BOOL)fileIsAppendOnly;
//8>
- (BOOL)fileIsImmutable;
//9>
- (NSDate *)fileModificationDate;
//10>
- (NSNumber *)fileOwnerAccountID;
//11>
- (NSString *)fileOwnerAccountName;
//12>
- (NSUInteger)filePosixPermissions;
//13>
- (unsigned long long)fileSize;
//14>
- (NSUInteger)fileSystemFileNumber;
//15>
- (NSInteger)fileSystemNumber;
//16>
- (NSString *)fileType;
12缭乘、創(chuàng)建描述
//1>將dictionary轉(zhuǎn)換為string
@property(readonly, copy) NSString *description;
//2>將dictionary轉(zhuǎn)換為string(不太懂文檔上寫的沐序,區(qū)別就在于上一個(gè)有{},這個(gè)沒有)
@property(readonly, copy) NSString *descriptionInStringsFileFormat;
//3>
- (NSString *)descriptionWithLocale:(id)locale;
//4>
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;
13堕绩、初始化
- (instancetype)initWithCoder:(NSCoder *)aDecoder;
14策幼、實(shí)例方法
//1>
- (NSEnumerator<KeyType> *)keyEnumerator;
//2>
- (void)getObjects:(ObjectType _Nonnull [])objects andKeys:(KeyType _Nonnull [])keys count:(NSUInteger)count;