5、NSValue、NSNumber、NSDictionary

5、NSValue、NSNumber、NSDictionary

.h
#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;

@end
.m
#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:@"name = %@ age = %ld", _name, _age];
}

@end
main.m
#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        // NSArray只能保存对象类型的数据,无法保存基本类型的数据

        // NSNumber NSValue
        // 如果想要保存基本类型的数据,需要将基本类型的数据转换为对象类型,使用NSNumber或NSValue类
        NSString *name = @"生气大炮哥";
        char gender = 'f';
        int age = 19;
        BOOL b = YES;
        float score = 78.5;

        // 将非对象类型转换为对象类型
        // 初始化
        NSNumber *genderNumber1 = [[NSNumber alloc] initWithChar:gender];
        // 便利构造器
//        NSNumber *genderNumber2 = [NSNumber numberWithChar:gender];
        // 字面量
//        NSNumber *genderNumber3 = @(gender);

        // 非对象转换为对象
        NSNumber *ageNumber = [NSNumber numberWithInt:age];
        NSNumber *bNumber = [NSNumber numberWithBool:b];
        NSNumber *scoreNumber = [NSNumber numberWithFloat:score];

        // 存入可变数组
        NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:ageNumber, bNumber, scoreNumber, genderNumber1, name, nil];

        NSLog(@"%@", mutableArray);

        // 取出年龄
        NSNumber *ageNumber2 = mutableArray[0];
        int age3 = [ageNumber2 intValue];  // 想要转什么类型直接发送消息
        age3 += 1;

        // NSNumber无法包装结构体变量,需要使用NSValue类的类方法
        NSRange range = NSMakeRange(10, 20);
        NSValue *rangeObject = [NSValue valueWithRange:range];
        NSLog(@"%@", rangeObject);

        NSRange range2 = [rangeObject rangeValue];
        NSUInteger l = range2.length;
        NSLog(@"%lu", l);



        // NSDictionary 字典类
        // 字典中存储是无序的
        // 字典中的元素是以键值对的形式存在的
        // 获取字典的值需要用key ,不可以用下标
        // key不能重复

        // 使用字面量
        NSDictionary *dict = @{
                               @"key": @"value",
                               @"k2": @"v2",
                               @"k3": @"v3"
                               };
        NSLog(@"%@", dict[@"k2"]);
        NSLog(@"%@", dict[@"k211"]);

        // 使用初始化方法(所有的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"];


        // 创建对象
        Person *p1 = [Person personWithName:@"同桌" age:23];
        Person *p2 = [Person personWithName:@"前桌" age:24];
        Person *p3 = [Person personWithName:@"后桌" age:19];
        Person *p4 = [Person personWithName:@"隔壁桌" age:25];

        // 创建空的可变字典
        NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];

        // 添加键值对
        [mutableDict setObject:p1 forKey:@"tong"];   // 添加、覆盖的作用
        [mutableDict setObject:p2 forKey:@"qian"];
        [mutableDict setObject:p3 forKey:@"hou"];
        [mutableDict setObject:p4 forKey:@"gebi"];

        NSLog(@"%@", mutableDict);
        NSLog(@"%@", [p1 description]);

        // setObject: forKey  特定情况具有特定含义
        Person *p5 = [Person personWithName:@"gg" age:34];
        [mutableDict setObject:p5 forKey:@"gebi"];
        NSLog(@"%@", mutableDict);

        // 删除
        [mutableDict removeObjectForKey:@"gebi"];
        NSLog(@"%@", mutableDict);

        // 将参数字典的所有元素覆盖掉元字典的所有元素
        [mutableDict setDictionary:@{
                                     @"key": @"value"
                                     }];
        NSLog(@"%@", mutableDict);


        // 遍历字典
        NSMutableDictionary *mutableDict1 = [NSMutableDictionary dictionaryWithObjects:@[@"v1", @"v2"] forKeys:@[@"k1", @"k2"]];

        for (int i = 0; i < mutableDict1.count; i++) {

            // 获取一个key
            NSString *key = mutableDict1.allKeys[i];

            // 获取对应的value
            NSString *value = mutableDict1[key];  // or  mutableDict1.allValues[i]

            NSLog(@"key = %@ value = %@", key, value);
        }

        // 使用便利构造器创建集合
        NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"a", nil];
        NSLog(@"%@", set1);

        // 使用数组创建集合
        NSArray *array = @[@"12", @"34"];
        NSSet *set2 = [NSSet setWithArray:array];
        NSLog(@"%@", set2);

        // 打印集合元素个数
        NSLog(@"%lu", set1.count);

        // 创建空的可变集合
        NSMutableSet *mutableSet = [NSMutableSet set];

        // 添加两个不同元素
        [mutableSet addObject:@"aa"];
        [mutableSet addObjectsFromArray:@[@"aa", @"bb"]];
        NSLog(@"%@", mutableSet);

        // 删除元素
        [mutableSet removeObject:@"aa"];
//        [mutableSet removeAllObjects];
        NSLog(@"%@", mutableSet);

        // 通过集合计算重复元素的个数
        NSArray *ages = @[@12, @13, @34, @12, @12, @34, @34.5, @12];
        NSCountedSet *agesSet = [NSCountedSet setWithArray:ages];
        NSUInteger count = [agesSet countForObject:@12];   // 获取重复对象的个数
        NSLog(@"%lu", count);

        for (NSNumber *num in agesSet) {
            NSLog(@"%@", num);
        }                        
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值