码迷,mamicode.com
首页 > 其他好文 > 详细

NSNumber(封装基本数据类型)

时间:2015-01-27 21:57:46      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

//
//  main.m
//  OC05-task-04
//
//  Created by Xin the Great on 15-1-26.
//  Copyright (c) 2015年 Xin the Great . All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        
        //////////////////////NSNumber//////////////////////
        //封装基本数据类型
        
        int intValue = 100;
        float floatValue = 3.14;
        BOOL boolValue = YES;
        
        NSNumber *intNumber = [[NSNumber alloc] initWithInt:intValue];
        NSLog(@"intNumber is %@", intNumber);
        NSNumber *floatNumber = [NSNumber numberWithFloat:floatValue];
        NSLog(@"floatNumber is %@", floatNumber);
        NSNumber *boolNumber = [NSNumber numberWithBool:boolValue];
        NSLog(@"boolNumber is %@", boolNumber);

        //包装之后就可以放到容器中
        NSArray *arr = @[intNumber, floatNumber, boolNumber];
        NSLog(@"arr is %@", arr);
        
        
        //还原成基本数据类型
        float value = [floatNumber floatValue];
        int value2 = [intNumber intValue];
        NSLog(@"value is %.2f",value);
        NSLog(@"value2 is %d",value2);
        
        
        //快速创建的方法
        NSNumber *intNum = @80;//---->  NSNumber *intNumber = [[NSNumber alloc] initWithInt:80];
        
        NSNumber *floatNum = @3.14;// ---->[NSNumber numberWithFloat:3.14];

        NSNumber *boolNum = @YES;// ---> [NSNumber numberWithBool:YES];
        
        NSNumber *charNum = @'a'; // ---> [NSNumber numberWithChar:'a'];
        
        
        //运算
        NSNumber *number = @(1 + 2);
        
        //直接在数组中写
        NSArray *arr1 = @[@"string", @12, intNum, floatNum, boolNum, charNum, number];
        NSLog(@"arr1 is %@", arr1);
        
    }
    return 0;
}

NSNumber(封装基本数据类型)

标签:

原文地址:http://blog.csdn.net/zuojx1013/article/details/43202413

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!