本文介绍: NSDictionary集合由多组keyvalue组成,因此创建NSDictionary需要同时指定多组keyvalue对。 NSDICTIONARY分别提供了类方法实例方法创建NSDictionary。一旦得到NSDictionary对象接下来可以通过方法访问集合包含keyvalue。下面为NSDictionary扩展一个print类别。上面的程序示范了NSDictionary两个基本用法程序使用快速枚举遍历NSDictionary的所有key。也可以根据key获取对应N

NSDictionary的功能用法

NSDictionary集合由多组keyvalue组成,因此创建NSDictionary时需要同时指定多组keyvalue对。 NSDICTIONARY分别提供了类方法实例方法创建NSDictionary。

一旦得到NSDictionary对象,接下来就可以通过方法访问该集合所包含的 key或value。

下面为NSDictionary扩展一个print类别。

#import <Foundation/Foundation.h&gt;

@interface NSDictionary (print)

- (void) print;

@end

@implementation NSDictionary (print)

- (void) print {
    NSMutableString* result = [NSMutableString stringWithString:@"{"];
    for (id key in self) {
        [result appendString:[key description]];
        [result appendString:@"="];
        [result appendString:[self[key] description]];
        [result appendString:@", "];
    }
    NSUInteger len = [result length];
    [result deleteCharactersInRange:NSMakeRange(len - 2, 2)];
    [result appendString:@"}"];
    NSLog(@"%@", result);
}
@end

上面的程序示范了NSDictionary的两个基本用法程序可使用快速枚举遍历NSDictionary的所有key。也可以根据key来获取对应NSDictionary中的value。

对NSDictionary的key排序

NSDictionary还提供了方法对NSDictionary的key执行排序,这些方法执行完成后将返回排序完成后的所有key组成的NSArray。

下面程序将示范使用代码块排序

#import <Foundation/Foundation.h>
#import "NSDictionary+print.h"

int main(void) {
    @autoreleasepool {
        NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Objective-c", @"one", @"Rudy", @"two", @"Python", @"three", nil];
        // 打印dict集合所有元素
        [dict print];
        NSArray* keyArr = [dict keysSortedByValueUsingComparator:
                    ^(id value1, id value2) {
            if ([value1 length] > [value2 length]) {
                return NSOrderedDescending;
            }
            if ([value1 length] < [value2 length]) {
                return NSOrderedAscending;
            }
            return NSOrderedSame;
        }];
        NSLog(@"%@", keyArr);
    }
}

代码块中的比较规则是,value对应字符串越长,系统就认为该value值越大。

编译运行程序即可看到如下输出

2022-06-12 15:51:36.930684+0800 oc.programme[94349:95375889] {one=Objective-c, two=Rudy, three=Python}
2022-06-12 15:51:36.931119+0800 oc.programme[94349:95375889] (
    two,
    three,
    one
)
Program ended with exit code: 0

对NSDicttionary的key进行过滤

NSDictionary还提供方法对所有的key执行过滤,这些方法返回满足过滤条件的key组成的NSSet。

如下程序示范:

int main(void) {
    @autoreleasepool {
        NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithInt:89], @"Objective-C",
                              [NSNumber numberWithInt:69], @"Rudy",
                              [NSNumber numberWithInt:75], @"Python",
                              [NSNumber numberWithInt:109], @"Perl", nil];
        // 打印dict集合的所有元素
        [dict print];
        // 对NSDictionary的所有key进行过滤
        NSSet* keySet = [dict keysOfEntriesPassingTest:
                          ^(id key, id value, BOOL* stop) {
            // 当value的值大于80时才返回YES
            return (BOOL)([value intValue] > 80);
        }];
        NSLog(@"%@", keySet);
    }
}

编译运行该程序,可以看到如下输出

2022-06-12 16:22:34.784943+0800 oc.programme[4789:101091183] {Perl=109, Rudy=69, Objective-C=89, Python=75}
2022-06-12 16:22:34.785305+0800 oc.programme[4789:101091183] {(
    Perl,
    "Objective-C"
)}
Program ended with exit code: 0

NSMutableDictionary 的功能用法

NSMutableDictionary继承NSDictionary,它代表一个key-value可变的NSDictionary集合。

NSMutableDictionary主要新增如下方法:

  • setObject:forKey: 设置一个key-value对。如果NSMutableDictionary中没有包含与该key相同的key-value,NSMutableDictionary将会新增一个key-value对;否则该key-value对将会覆盖已有的key-value对。
  • setObject:forKeyedSubscript: 通过该方法支持,允许程序通过下标设置key-value对。
  • addEntruesFromDictionary: 将另一个 NSDictionary中所有的key-value对复制当前NSDictionary中。
  • setDictionary: 将另一个 NSDictionary中所有的key-value对替换当前NSDictionary的key-value对。
  • removeObjectForKey: 根据key来删除key-value对。
  • removeAllObject: 清空该NSDictionary。
  • removeObjectsForKeys: 使用多个key组成的NSArray作为参数,同时删除多个key对应的key-value对。

原文地址:https://blog.csdn.net/m0_63852285/article/details/125244642

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_36772.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注