• NSDictionary所有API的学习。


    <欢迎大家增加iOS开发学习交流群:QQ529560119>

    @property (readonly)NSUInteger count;

    //1.利用指定的key寻找相应的value

    - (id)objectForKey:(id)aKey;

    //2. keyEnumerator得到一个字典的全部键值

    - (NSEnumerator *)keyEnumerator;

    //3.初始化字典

    - (instancetype)initNS_DESIGNATED_INITIALIZER;

    //4.条件编译依据不同情况来初始化字典

    #if TARGET_OS_WIN32

    - (instancetype)initWithObjects:(constid [])objects forKeys:(constid [])keys count:(NSUInteger)cnt;

    #else

    - (instancetype)initWithObjects:(constid [])objects forKeys:(constid <NSCopying> [])keys count:(NSUInteger)cntNS_DESIGNATED_INITIALIZER;

    #endif

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

    @end

    @interface NSDictionary (NSExtendedDictionary)

    //5.数组全部key属性

    @property (readonly,copy) NSArray *allKeys;

    //6.依据所填入的object返回相应全部的key键值

    - (NSArray *)allKeysForObject:(id)anObject;


    //7.属性 字典全部value  

    @property (readonly,copy) NSArray *allValues;

    //8.属性 字符串描写叙述

    @property (readonly,copy) NSString *description;

    //9.属性 字符串描写叙述文件格式

    @property (readonly,copy) NSString *descriptionInStringsFileFormat;

    //10.依据设置的locale进行连接数组

    - (NSString *)descriptionWithLocale:(id)locale;

    //11.依据设置的locale进行连接数组

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

    //12.推断字典是否相等

    - (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary;

    //13.得到一个字典的全部values

    - (NSEnumerator *)objectEnumerator;

    //14.字典将某个特定的数组作为key值传进去得到相应的value,假设某个key找不到相应的key,就用notFoundMarker提前设定的值取代

    - (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)marker;

        //NSDictionary *dic=[NSDictionarydictionaryWithObjectsAndKeys:@"K1",@"V1",@"K2",@"V2",@"K3",@"V3",nil];

        //NSArray *arr1=[NSArrayarrayWithObjects:@"V1",@"V2",@"VG",nil];

        //NSArray *ARR= [dicobjectsForKeys:arr1 notFoundMarker:@"BB"];

        //NSLog(@"測试測试%@",ARR);

        //打印:

        //2015-06-08 11:30:54.139 NSDictionary[1624:64989]測试測试(

       //K1,

       //BB,

       //BB

        //)

    //15.将字典写进特定的路径path

    - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

    - (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;// the atomically flag is ignored if url of a type that cannot be written atomically.


    //16.字典依照value的大小顺序来对keys键值进行排序(通过value排序,返回key集合)

    - (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator;

    //NSDictionary *dic1=[NSDictionarydictionaryWithObjectsAndKeys:@"4",@"A",@"6",@"C",@"5",@"B",nil];

        //NSArray *arr2= [dic1keysSortedByValueUsingSelector:@selector(compare:)];

        //NSLog(@"奇葩奇葩%@",arr2);

    //2015-06-08 14:41:59.152 NSDictionary[2749:117502]奇葩奇葩(

       //A,

       //B,

       //C

    //)

    //17.

    - (void)getObjects:(id__unsafe_unretained [])objects andKeys:(id__unsafe_unretained [])keys;

    //18.

    - (id)objectForKeyedSubscript:(id)keyNS_AVAILABLE(10_8,6_0);


    //19.利用block对字典进行遍历

    - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,id obj, BOOL *stop))blockNS_AVAILABLE(10_6,4_0);

    //样例:

        NSDictionary *dic = [NSDictionarydictionaryWithObjects:@[@"1",@"2",@"3"]forKeys:@[@"one",@"two",@"three"]];

       NSString *stopKey = @"two";

       __block BOOL stopEarly =NO;

        [dicenumerateKeysAndObjectsUsingBlock:^(id key,id obj, BOOL *stop) {

           NSLog(@"%@,%@",key,obj);

            //訪问对象类型变量

           if ([key isEqualToString:stopKey]) {

                *stop =YES;

                //訪问__block表识的局部类型变量

                stopEarly =YES;

               //直接訪问属性

                NSLog(@"self.name = tom");

                ;

            }

        }];

    //输出:

    2015-06-08 15:19:09.608 NSDictionary[3035:136164] one,1

    2015-06-08 15:19:09.609 NSDictionary[3035:136164] two,2

    2015-06-08 15:19:09.609 NSDictionary[3035:136164] self.name = tom


    //20.同上一样利用block对字典进行遍历,只是加了排序的顺序选项options正反序

    - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id key, id obj,BOOL *stop))block NS_AVAILABLE(10_6, 4_0);


    //21.和第16一样都是利用valuekeys进行排序,仅仅只是这个加上了一个可设定的NSComparato參数条件来比較

    - (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptrNS_AVAILABLE(10_6,4_0);

    //样例

       NSArray *sortedKeys = [dic keysSortedByValueUsingComparator:^NSComparisonResult(id obj1,id obj2) {

           if ([obj1 integerValue] > [obj2integerValue]) {

                return (NSComparisonResult)NSOrderedAscending;

            }

           if ([obj1 integerValue] < [obj2integerValue]) {

                return (NSComparisonResult)NSOrderedDescending;

            }

            return (NSComparisonResult)NSOrderedSame;

        }];

        NSLog(@"利用keysSortedByValueUsingComparator进行排序%@",sortedKeys);

    //输出:

    2015-06-08 16:07:12.361 NSDictionary[3420:160942]利用keysSortedByValueUsingComparator进行排序(

        one,

        three,

        two

    )

    //22.通过values对字典的keys进行排序。能够有排序的选择,还可加入设定的NSComparato參数条件来比較

    - (NSArray *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptrNS_AVAILABLE(10_6,4_0);

    //23.这是一个非常好的对字典进行过滤的方法,返回keys的集合,这些keys符合參数block的约束,在block内部在特定的条件下返回yes,返回的这个集合会保留当前遍历到那个字典对象的信息

    - (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key,id obj, BOOL *stop))predicateNS_AVAILABLE(10_6,4_0);

    //样例:

    NSDictionary * numsDic =@{@(2):@"second",@(4):@"first",@(1):@"thrid"};

       NSSet * filteredKeys = [numsDic keysOfEntriesPassingTest:^BOOL(id key,id obj, BOOL *stop) {

           BOOL result = NO;

           NSNumber * numKey = key;

           if (numKey.integerValue >2) {

                result =YES;

            }

           return YES;

        }];

        NSLog(@"filteredKeys.description----%@",filteredKeys.description);

    //打印:

    2015-06-08 17:34:37.741 NSDictionary[4085:193311] filteredKeys.description----{(

        4

    )}


    //23.使用方法同上,添加了一个列举的选项选择

    - (NSSet *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id key, id obj,BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);

    @end


    @interface NSDictionary (NSDictionaryCreation)

    //24.高速创建一个空字典

    + (instancetype)dictionary;

    //25.高速创建字典而且赋初值

    + (instancetype)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key;

    //26.条件编译 不同情况创建字典的几种方法

    #if TARGET_OS_WIN32

    + (instancetype)dictionaryWithObjects:(constid [])objects forKeys:(constid [])keys count:(NSUInteger)cnt;

    #else

    + (instancetype)dictionaryWithObjects:(constid [])objects forKeys:(constid <NSCopying> [])keys count:(NSUInteger)cnt;

    #endif

    + (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...NS_REQUIRES_NIL_TERMINATION;

    //27.创建新字典 赋值一个字典

    + (instancetype)dictionaryWithDictionary:(NSDictionary *)dict;

    //28.创建字典。通过数组赋值valueskeys

    + (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;

    //29.使用指定的以nil为结尾的对象与键对列表初始化列表

    - (instancetype)initWithObjectsAndKeys:(id)firstObject, ...NS_REQUIRES_NIL_TERMINATION;

    //30.使用还有一个字典初始化字典

    - (instancetype)initWithDictionary:(NSDictionary *)otherDictionary;

    //31.使用还有一个字典初始化字典,还能够为每一个对象创建新的副本

    - (instancetype)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag;

    //32.使用指定的对象与键初始化字典

    - (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;

    //33.使用本地文件的内容初始化字典

    + (NSDictionary *)dictionaryWithContentsOfFile:(NSString *)path;

    //34.使用URL的内容初始化字典

    + (NSDictionary *)dictionaryWithContentsOfURL:(NSURL *)url;

    //35.使用本地文件的内容初始化字典

    - (NSDictionary *)initWithContentsOfFile:(NSString *)path;

    //36.使用URL的内容初始化字典

    - (NSDictionary *)initWithContentsOfURL:(NSURL *)url;

    @end


    /**************** Mutable Dictionary****************/

    @interface NSMutableDictionary :NSDictionary

    //37.依据相应的key删除相应的value以及自身的key

    - (void)removeObjectForKey:(id)aKey;

    //38.在可变字典中,改变相应的key的value

    - (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;

    //39.创建字典初始化

    - (instancetype)initNS_DESIGNATED_INITIALIZER;

    //40.初始化字典而且指定大小

    - (instancetype)initWithCapacity:(NSUInteger)numItemsNS_DESIGNATED_INITIALIZER;

    //41.序列化对象

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

    @end


    @interface NSMutableDictionary (NSExtendedMutableDictionary)

    //42.一个字典总体拼接还有一个字典的方法

    - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;

    //43.删除字典全部的数据

    - (void)removeAllObjects;

    //44.依据指定的数据keys删除相应的values

    - (void)removeObjectsForKeys:(NSArray *)keyArray;

    //45.给可变字典加入一组新字典

    - (void)setDictionary:(NSDictionary *)otherDictionary;

    //46.以数组下标的形式来訪问对应键的值

    - (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)keyNS_AVAILABLE(10_8,6_0);

    @end


    @interface NSMutableDictionary (NSMutableDictionaryCreation)

    //47.高速创建可变字典而且初始化大小

    + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;

    //48.高速创建可变字典通过指定的文件路径

    + (NSMutableDictionary *)dictionaryWithContentsOfFile:(NSString *)path;

    //49.高速创建可变字典通过URL

    + (NSMutableDictionary *)dictionaryWithContentsOfURL:(NSURL *)url;

    //50.使用本地文件的内容创建可变字典

    - (NSMutableDictionary *)initWithContentsOfFile:(NSString *)path;

    //51.使用URL的内容创建可变字典

    - (NSMutableDictionary *)initWithContentsOfURL:(NSURL *)url;

    @end


    @interface NSDictionary (NSSharedKeySetDictionary)

    //52.用来创建预订好的key集合。返回的值作为NSMutableDictionarydictionaryWithSharesKeySet參数传入,能够重用key,从而节约copy操作,节省内存。

    + (id)sharedKeySetForKeys:(NSArray *)keysNS_AVAILABLE(10_8,6_0);

    @end


    @interface NSMutableDictionary (NSSharedKeySetDictionary)

    //53.创建字典,共享键集会复用对象

    + (NSMutableDictionary *)dictionaryWithSharedKeySet:(id)keysetNS_AVAILABLE(10_8,6_0);

    @end


  • 相关阅读:
    转:Metronic – 超赞!基于 Bootstrap 的响应式后台管理模板
    [km] 如何判断一个直播系统是否使用的是RTMP
    xshell5.0实现中键复制
    [转] 腾讯云直播OBS推流教程
    eclipse格式化代码快捷键失效
    JAVA注解@Interface基础知识
    MySQL按中文拼音排序
    Navicat连不上mysql8
    MySQL重置root密码
    CentOS7防火墙之firewalld
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5277981.html
Copyright © 2020-2023  润新知