美文网首页
OC -- NSNumber

OC -- NSNumber

作者: J_coco | 来源:发表于2016-01-23 16:25 被阅读0次

    基本数据类型:
    int long NSInteger
    float double
    BOOl
    由于不是对象类型,所以OC中的数组和字典都不能保存

    NSNumber -> 将任意基本数据类型封装成对象类型

    //1.创建
      //基本数据类型 ===>  对象
    
    //空
    NSNumber *number = [[NSNumber alloc]init];
    
    //int
    NSNumber *int_ =  [[NSNumber alloc]initWithInt:10];
    
    //float
    NSNumber *float_ = [[NSNumber alloc]initWithFloat:3.14];
    
    //double
    NSNumber *double_ = [[NSNumber alloc]initWithDouble:3.14];
    
    //BOOL
    NSNumber *bool_ [[NSNumber alloc]initWithBool:YES];
    
    //char
    NSNumber *char_ = [[NSNumber alloc]initWithChar:'C'];
    
    NSLog(@"%@",int_);
    
    
    //2.快速创建:@...
    NSNumber *_int = @100;
    NSNumber *_float = @3.14;
    NSNumber *_bool = @YES;
    NSNumber *_char = @'c';
    NSNumber *_double = @(3.14 * 6 + 2.5);
    
    
    //3.NSNuber可以放入容器中
    NSArray *array = @[_int,_float,_bool,_char];
    NSDictionary *dic = @{@"key1":_int,@"key2":_double};
    
    
    //4.NSNumber ==>基本数据类型
    int a = [_int intValue];
    float b = [_float floatValue];
    char c = [_char charValue];
    double = [_double doubleValue]
    

    相关文章

      网友评论

          本文标题:OC -- NSNumber

          本文链接:https://www.haomeiwen.com/subject/wajrkttx.html