Foundition API - NSDictionary學(xué)習(xí)

第一次學(xué)習(xí)英文官方文檔,有很多不懂得地方,暫時(shí)空著,回頭查詢中文文檔再補(bǔ)全,如果有大神愿意指點(diǎn),請(qǐng)留言,感謝!?。?/p>

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è)絕對(duì)路徑或者是相對(duì)路徑,由這個(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的順序必須一一對(duì)應(yīng)

//7>?。。。。。。。。?!沒搞清楚!?。?!
+ (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];

//?。。。。。。。。?!沒搞懂
//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>?。。。?!沒搞懂
- (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.!?。。?!沒搞懂

+ (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.不懂?。。。。。。?!
- (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];
//如果找到對(duì)應(yīng)的key值,則返回對(duì)應(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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評(píng)論 19 139
  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,068評(píng)論 0 9
  • 一、源題QUESTION 1The instance abnormally terminates because ...
    貓貓_tomluo閱讀 1,751評(píng)論 0 2
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,728評(píng)論 18 399
  • 千里煙波瀲滟生,蘆荻簌簌雁清鳴。蒼山遠(yuǎn)影連空碧,一葉扁舟獨(dú)釣萍。 云蕩翼,雨飄零,此心歸棹踏歌行。天涯目斷情難斷,...
    易詞齋主人閱讀 1,696評(píng)論 38 44

友情鏈接更多精彩內(nèi)容