字典

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        
        // NSArray只能保存对象类型的数据,无法保存基本类型数据
        // 如果想要保存基本类型数据,需要将基本数据类型转为对象类型,使用NSNumber或者NSValue类
        /*
        NSString *name = @"神奇大爆哥";
        char gender = 'a';
        int age = 18;
        BOOL b = YES;
        float score = 98.4;
        
        // 将非对象类型转为对象类型
        NSNumber *gender2 = [[NSNumber alloc] initWithChar:gender];
        NSNumber *age2 = [NSNumber numberWithInt:age];
        NSNumber *b2 = @(b);
        NSNumber *score2 = @(score);
        
        
        // 存入可变数组,并打印
        NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:name, gender2, age2, b2, score2, nil];
        NSLog(@"%@", mutableArray);
        
        
        // 取出
        NSNumber *ageNumber = mutableArray[2];
        int age3 = [ageNumber intValue]; // 想要转为什么类型,直接发送消息
        age3 += 1;
        
        
        // NSNumber无法包装结构体变量,需要使用NSValue类的类方法
        NSRange range = NSMakeRange(10, 20);
        NSValue *rangeObject = [NSValue valueWithRange:range];
        [rangeObject rangeValue];
        */
        
        
        // NSDictionary:字典类
        // 字典中存储是无序的
        // 字典中的元素都是以键值对的形式存在
        // 获取字典中的值,需要通过key,不可以通过下标
        // 字典的key不能重复
        NSDictionary *dict = @{
                               @"key1": @"value1",
                               @"baoge": @"豹哥是个好孩子",
                               @"baomei": @"豹哥是个好女孩子"
                               };
        NSLog(@"%@", dict[@"baoge"]);
        
        // 初始化方法创建字典(所有的Value的数组和所有的Key的数组个数需要一致)
        NSDictionary *dict2 = [[NSDictionary alloc] initWithObjects:@[@"a", @"b"] forKeys:@[@"1", @"2"]];
        NSLog(@"%@", dict2);
        
        // 把所有键值对放在一个参数中,值在前,键在后
        NSDictionary *dict3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"a", @"b", @"c", @"d", nil];
        NSLog(@"%@", dict3);

        
        
        // 获取字典键值对的值
//        dict3[@"b"]
//        [dict3 objectForKey:@"b"];

        NSLog(@"%@", [dict3 allKeys]);
        NSLog(@"%@", [dict3 allValues]);
        
        
        
        // 1.创建对象
        Person *p1 = [Person personWithName:@"前桌" age:33];
        Person *p2 = [Person personWithName:@"同桌" age:31];
        Person *p3 = [Person personWithName:@"后桌" age:12];
        Person *p4 = [Person personWithName:@"介笔子" age:16];
        
        // 2.创建空的可变字典
        NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
        
        // 3.添加键值对
        [mutableDict setObject:p1 forKey:@"qian"];
        [mutableDict setObject:p2 forKey:@"tong"];
        [mutableDict setObject:p3 forKey:@"hou"];
        [mutableDict setObject:p4 forKey:@"jie"];
        NSLog(@"%@", mutableDict);
        
        // setObject:forKey: 特定情况,具有特定含义
        Person *p5 = [Person personWithName:@"linju" age:19];
        [mutableDict setObject:p5 forKey:@"jie"];
        NSLog(@"%@", mutableDict);
        
        // 删除
        [mutableDict removeObjectForKey:@"jie"];
        NSLog(@"%@", mutableDict);
        
        // 将参数字典的所有元素覆盖掉原字典所有元素
        [mutableDict setDictionary:@{
                                     @"key": @"value"
                                     }];
        NSLog(@"%@", mutableDict);
        
        
        
        NSMutableDictionary *numbersDict = [NSMutableDictionary dictionaryWithObjects:@[@"value1", @"value2"] forKeys:@[@"key1", @"key2"]];
        
        for (int i = 0; i < numbersDict.count; i++) {
            // 获取一个key
            NSString *key = numbersDict.allKeys[i];
            
            // 通过key获取value
//            NSString *value = numbersDict[key];
            NSString *value = numbersDict.allValues[i];
            
            NSLog(@"key: %@  value: %@", key, value);
        }
        
//    key: key1  value: value1
//    key: key2  value: value2
        
        
        
        
//        NSSet
        
        // 使用便利构造器创建集合
        NSSet *set1 = [NSSet setWithObjects:@"abc", @"bcd", @"abc", nil];
        NSLog(@"%@", set1);
        
        // 使用数组创建集合
        NSArray *array = @[@"12", @"12", @"18", @"16"];
        NSSet *set2 = [NSSet setWithArray:array];
        NSLog(@"%@", set2);
        
        // 打印集合元素个数
        NSLog(@"%ld", [set2 count]);
        
        // 创建空的可变集合
        NSMutableSet *mutableSet = [NSMutableSet set];
        
        // 添加两个不同元素
        [mutableSet addObject:@"hh"];
        [mutableSet addObjectsFromArray:@[@"mm", @"hh"]];
        NSLog(@"%@", mutableSet);
        
        // 删除元素
        [mutableSet removeObject:@"hh"];
        NSLog(@"%@", mutableSet);
        
        
        
        // 通过集合计算重复元素的个数
        NSArray *ages = @[@12, @34, @35.3, @(34.1), @12, @12, @12, @34];
        NSCountedSet *agesSet = [NSCountedSet setWithArray:ages];
        NSUInteger count = [agesSet countForObject:@12]; // 获取重复对象的个数
        NSLog(@"%lu", count);
        
        
        
//        for (int i = 0; i < ages.count; i++) {
//            NSLog(@"%@", ages[i]);
//        }
        
        for (NSNumber *number in ages) {
            
            NSLog(@"%@", number);
            
        }

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    NSString *_name;
    NSInteger _age;
}

- (void)setName:(NSString *)name;
- (NSString *)name;

- (void)setAge:(NSInteger)age;
- (NSInteger)age;

- (instancetype)initWithName:(NSString *)name
                         age:(NSInteger)age;

+ (instancetype)personWithName:(NSString *)name
                           age:(NSInteger)age;
#import "Person.h"

@implementation Person

- (void)setName:(NSString *)name
{
    _name = name;
}
- (NSString *)name
{
    return _name;
}

- (void)setAge:(NSInteger)age
{
    _age = age;
}
- (NSInteger)age
{
    return _age;
}

- (instancetype)initWithName:(NSString *)name
                         age:(NSInteger)age
{
    self = [super init];
    if (self) {
        _name = name;
        _age = age;
    }
    return self;
}

+ (instancetype)personWithName:(NSString *)name
                           age:(NSInteger)age
{
    return [[Person alloc] initWithName:name age:age];
}


// 重写description方法,目的是打印对象的时候,直接输出对象实例变量的值
- (NSString *)description
{
    return [NSString stringWithFormat:@"%@ %ld", _name, _age];
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值