Objective-C学习篇09—NSNumber与笑笑语法

时间:2022-04-28 11:51:44

  

NSNumber

  由于数组,字典,集这三个容器中只能存放对象类型的数据,如果想把基本数据类型的数据存放到这三个容器中,需要把基本数据类型转化为对象类型,此时就要借助于NSNumber 这个类.

  NSNumber 继承自NSValue, 数字类,主要的作用是将基本数据类型转换为NSNumber对象类型,也能将对象类型转换为基本数据类型

  整型

;

创建NSnumber对象 numberWithInt:

NSNumber *intNumber = [NSNumber numberWithInt:a];  即实现了将基本数据类型转化为对象类型
NSLog(@"intNumber = %@", intNumber);

反之,将对象类型转换为基本数据类型    .intValue

int b = intNumber.intValue; // 属性,相当于 [intNumber intvalue];

NSLog(@"b = %d", b);

2015-12-04 13:06:17.252 OCNSNumber[1659:136457] intNumber = 100

2015-12-04 13:06:17.253 OCNSNumber[1659:136457] b = 100

  浮点型 

    float c = 3.14;

    NSNumber *floatNumber = [NSNumber numberWithFloat:c];  numberWithFloat:
    NSLog(@"floatNumber = %@", floatNumber);

    float d = floatNumber.floatValue;   floatValue
    NSLog(@"d = %.2f", d);

  字符型

char e = 'a';

   字符类型的转化为数字对象     numberWithChar:

    NSNumber *charNumber = [NSNumber numberWithChar:e];

    NSLog(@"charNumber = %@", charNumber);

    char f = charNumber.charValue;  .charValue
NSLog(@"f = %c", f);

  短整型  

    ;

    NSNumber *shortNumber = [NSNumber numberWithLong:g];   numberWithLong:

    NSLog(@"shortNumber = %@", shortNumber);

    short h = shortNumber.shortValue;   .shortValue

    NSLog(@"h = %d", h);

  小练习

  // , n2 = , n3 = , n4 = ;

  要求:将上面四个变量放到数组对象中,然后排序

  // 转化为对象类型

    NSNumber *m1 = [NSNumber numberWithInt:n1];

    NSNumber *m2 = [NSNumber numberWithInt:n2];

    NSNumber *m3 = [NSNumber numberWithInt:n3];

    NSNumber *m4 = [NSNumber numberWithInt:n4];

  // 装进数组对象中

    NSArray *array = [NSArray arrayWithObjects:m1, m2, m3, m4, nil];

  // 对数组对象中的元素进行排序

    NSArray *sortArray = [array sortedArrayUsingSelector:@selector(compare:)];

    NSLog(@"%@", sortArray);

2015-12-04 13:15:23.908 OCNSNumber[1667:140688] (

10,

15,

18,

22

)

  总结: 基本数据类型转化为对象: 使用 [NSNumber numberWith+基本数据类型的类型名];反之,将对象类型转化为基本数据类型: 基本数据类型+Value;

笑笑语法

  下面是关于一些笑笑语法的使用

  笑笑语法,也就是字面量,是一种简洁表示数据的方式

  1.对象数据类型的数据表示可以直接使用 @

  比如: 拿100 来讲,一般的做法是:

NSNumber *number = [NSNumber numberWithInt:100];

NSLog(@"%@", number);

  使用笑笑语法可以这样打印对象

NSLog(@"%@", @100);

  2. 不可变数组的笑笑语法 使用 @[ ] 中括号中放对象

NSArray *nameArray2 = @[@"zhangSan", @"liSi", @"wangEr"];

NSLog(@"%@", nameArray2);

  3. 可变数组笑笑语法 @[].mutableCopy;

NSMutableArray *mArray2 = @[@"1", @"2", @"3"].mutableCopy;  // 可变的都加 .mutableCopy

NSLog(@"%@", mArray2);

  4. 使用笑笑语法直接通过数组名加下标进行赋值

mArray2[0] = @100;

NSLog(@"%@", mArray2);

/*

2015-12-04 13:27:55.632 OCNSNumber[1690:146656] (

100,

2,

3

)

*/

  5. 字典的笑笑语法

  通常

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"xiaoZe", @"1", @"kunLing", @"2", @"sunLi", @"3", nil];

NSLog(@"%@", dic);

  笑笑语法  @{ 键:值,... };

NSDictionary *dic2 = @{@"1":@"xiaoZe", @"2":@"kunLing", @"3":@"sunLi"};

NSLog(@"%@", dic2);

  6. 可变字典(只有可变才能修改) @{键:值,...}.mutableCopy

NSMutableDictionary *mDic = @{@"1":@"a", @"2":@"b", @"3":@"c"}.mutableCopy;

NSLog(@"%@", mDic);

[mDic setObject:@"A" forKey:@"1"];

NSLog(@"%@", mDic);

  7. 修改key值对应的value值

mDic[@"2"] = @"L";

NSLog(@"%@", mDic);

NSString *value = [mDic objectForKey:@"2"];

NSLog(@"%@", value);

  8.取出key值对应的value

NSLog(@"%@", mDic[@"3"]);

  本节打印结果:

2015-12-04 13:31:10.137 OCNSNumber[1700:148140] intNumber = 100

2015-12-04 13:31:10.138 OCNSNumber[1700:148140] b = 100

2015-12-04 13:31:10.138 OCNSNumber[1700:148140] floatNumber = 3.14

2015-12-04 13:31:10.138 OCNSNumber[1700:148140] d = 3.14

2015-12-04 13:31:10.139 OCNSNumber[1700:148140] charNumber = 97

2015-12-04 13:31:10.139 OCNSNumber[1700:148140] f = a

2015-12-04 13:31:10.139 OCNSNumber[1700:148140] shortNumber = 10

2015-12-04 13:31:10.139 OCNSNumber[1700:148140] h = 10

2015-12-04 13:31:10.139 OCNSNumber[1700:148140] (

10,

15,

18,

22

)

2015-12-04 13:31:10.139 OCNSNumber[1700:148140] 100

2015-12-04 13:31:10.140 OCNSNumber[1700:148140] 100

2015-12-04 13:31:10.140 OCNSNumber[1700:148140] (

zhangSan,

liSi,

wangEr

)

2015-12-04 13:31:10.140 OCNSNumber[1700:148140] (

1,

2,

3

)

2015-12-04 13:31:10.140 OCNSNumber[1700:148140] (

100,

2,

3

)

2015-12-04 13:31:10.140 OCNSNumber[1700:148140] {

1 = xiaoZe;

2 = kunLing;

3 = sunLi;

}

2015-12-04 13:31:10.140 OCNSNumber[1700:148140] {

1 = xiaoZe;

2 = kunLing;

3 = sunLi;

}

2015-12-04 13:31:10.140 OCNSNumber[1700:148140] {

1 = a;

2 = b;

3 = c;

}

2015-12-04 13:31:10.141 OCNSNumber[1700:148140] {

1 = A;

2 = b;

3 = c;

}

2015-12-04 13:31:10.155 OCNSNumber[1700:148140] {

1 = A;

2 = L;

3 = c;

}

2015-12-04 13:31:10.155 OCNSNumber[1700:148140] L

2015-12-04 13:31:10.155 OCNSNumber[1700:148140] c