Foundition API - NSDictionary学习

2017-02-03  本文已影响0人  夜未殇

第一次学习英文官方文档,有很多不懂得地方,暂时空着,回头查询中文文档再补全,如果有大神愿意指点,请留言,感谢!!!

1、创建一个dictionary

//1>直接创建一个空dictionary
+ (instancetype)dictionary;
//demo
NSDictionary *dict = [NSDictionary dictionary];

//2>获取本地路径下文件中的dictionary
+ (NSDictionary<KeyType,ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path;
//demo
NSDictionary *dict1 = [NSDictionary dictionaryWithContentsOfFile:path];
//path是一个绝对路径或者是相对路径,由这个路径获取的文件内容必须是一个字典,否则dict1为nil。

//3>获取接口返回的dictionary
+ (NSDictionary<KeyType,ObjectType> *)dictionaryWithContentsOfURL:(NSURL *)url;
//demo
NSDictionary *dict1 = [NSDictionary dictionaryWithContentsOfURL:url];
//url是一个接口,由这个接口获取的文件内容必须是一个字典,否则dict1为nil。

//4>将一个dictionary中的key和value复制到一个新的dictionary中
+ (instancetype)dictionaryWithDictionary:(NSDictionary<KeyType,ObjectType> *)dict;
//demo
NSDictionary *dict1 = [NSDictionary dictionaryWithDictionary:dict];
//dict:一个dictionary

//5>创建一个只有一个key和value的dictionary
+ (instancetype)dictionaryWithObject:(ObjectType)object forKey:(id<NSCopying>)key;
//demo
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:value forKey:key];

//6>创建一个有多个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的顺序必须一一对应

//7>!!!!!!!!!!没搞清楚!!!!
+ (instancetype)dictionaryWithObjects:(const ObjectType  _Nonnull [])objects forKeys:(const id<NSCopying>  _Nonnull [])keys count:(NSUInteger)cnt;

//8>创建一个有多个value和key的dictionary
+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
//demo
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:value1,key1,value2,key2,value3,key3, nil];

2、初始化dictionary实例

//1>实例化一个空dictionary
- (instancetype)init;
//demo
NSDictionary *dict = [[NSDictionary alloc] init];

//2>实例化一个dictionary获取本地路径下文件中的数组
- (NSDictionary<KeyType,ObjectType> *)initWithContentsOfFile:(NSString *)path;
//demo
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

//3>实例化一个dictionary接收接口返回的字典
- (NSDictionary<KeyType,ObjectType> *)initWithContentsOfURL:(NSURL *)url;
//demo
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL:url];

//4>实例化一个dictionary,并将里一个数组中的元素复制
- (instancetype)initWithDictionary:(NSDictionary<KeyType,ObjectType> *)otherDictionary;
//demo
NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:dict1];

//!!!!!!!!!!没搞懂
//5>实例化一个dictionary,并将里一个数组中的元素复制(copyItems)
- (instancetype)initWithDictionary:(NSDictionary<KeyType,ObjectType> *)otherDictionary copyItems:(BOOL)flag;

//6>实例化一个有多个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>实例化一个有多个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、字典中条目的个数

@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的相关方法

//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(不知道和上一个方法的具体区别是啥,暂时知道都能获取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];
//如果找到对应的key值,则返回对应的value数组,若没找到,返回只有marker的数组

//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中处理字典中的数据
- (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中处理字典中的数据(暂时不清楚NSEnumerationReverse和NSEnumerationConcurrent的区别)
- (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、分类字典(暂时不懂,遇见再研究)

//1>
- (NSArray<KeyType> *)keysSortedByValueUsingSelector:(SEL)comparator;

//2>
- (NSArray<KeyType> *)keysSortedByValueUsingComparator:(NSComparator)cmptr;

//3>
- (NSArray<KeyType> *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr;

9、字典过滤(暂时不懂,遇见再研究)

//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、保存字典进一个文件

//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、创建描述

//1>将dictionary转换为string
@property(readonly, copy) NSString *description;

//2>将dictionary转换为string(不太懂文档上写的,区别就在于上一个有{},这个没有)
@property(readonly, copy) NSString *descriptionInStringsFileFormat;

//3>
- (NSString *)descriptionWithLocale:(id)locale;

//4>
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;

13、初始化

- (instancetype)initWithCoder:(NSCoder *)aDecoder;

14、实例方法

//1>
- (NSEnumerator<KeyType> *)keyEnumerator;

//2>
- (void)getObjects:(ObjectType  _Nonnull [])objects andKeys:(KeyType  _Nonnull [])keys count:(NSUInteger)count;
上一篇下一篇

猜你喜欢

热点阅读