iOS-常量与变量

“ 基本存储类型:32位 CPU 对应的字节大小”

数据类型 储存空间大小(单位:字节) 范围
Byte(字节型) 1 0 - 255
Boolean(布尔型/逻辑型) 2 True 或 False
Integer(整数型) 2 -32,768 ~ 32767
Long(长整型) 4(有符号32位整数) -2147483648 ~ 2147483647
Long Long(长长整型) 8(有符号64位整数) -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,80
Single(单精度浮点型) 4 负数范围:-3.402823E38 ~ -1.401298E-45

正数范围:1.401298E-45 ~ 3.402823E38
Double(双精度浮点型) 8 负数范围:-1.797,693,134,862,32E308 ~ -4.940,656,458,412,47E-324

正数范围: 4.940,656,458,412,47E-324 ~ 1.797,693,134,862,32E308
Object(对象型) 4 任何 Object 引用
String (变长) 10 长度从 0 到 大约 20 亿
String (定长) 10 长度从 1 到大约 65,400

#pragma mark --- 基础数据类型

/** 2.1:数据类型的相互转换
 *  NSString、int、float 的相互转换
 */
-(void)shujuTypeTransform1
{
    //1. NSString --> char*
    NSString *strr = @"Test转化123";
    const char * charA = [strr UTF8String];
    NSLog(@"charA = %s",charA);

    //2.char* --> NSString
    NSString *charToString = [NSString stringWithUTF8String:charA];
    NSLog(@"charToString= %@ \n ",charToString);

    NSString *newString = @"123456";
    //3.NSString --> int
    int Int = [newString intValue];

    //4.int      --> NSString
    NSString *Str = [NSString stringWithFormat:@"%d",Int];

    //5.NSString --> float
    float Float = [Str floatValue];

    //76.float   --> NSString
    NSString *str = [NSString stringWithFormat:@"%f",Float];
    

    NSLog(@"--->newString: %@",newString);
    NSLog(@"NSString ---> int      : %d",Int);
    NSLog(@"int      ---> NSString : %f",Float);
    NSLog(@"NSString ---> float    : %@",Str);
    NSLog(@"float    ---> NSString : %@\n ",str);

}
/** 2.2:数据类型的相互转换:
 *  NSData、NSString、Byte、UIImage、NSMutableData
 */
-(void)shujuTypeTransform2
{
    //1:NSString --> NSData
    NSString * str1 = @"hello, world!";
    NSData * data = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    //NSData --> NSString
    NSString * dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"---<2.1>data.bytes: %s", data.bytes);
    NSLog(@"---<2.1>dataString: %@\n ", dataString);

    
    //2:NSData --> Byte
    NSString *testString = @"1234567890";
    NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
    Byte *DataByte = (Byte *)[testData bytes];
    //Byte --> NSData
    Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
    NSData *Bytedata = [[NSData alloc] initWithBytes:byte length:24];
    NSLog(@"---<2.2>testByte: %s", DataByte);
    NSLog(@"---<2.2>Bytedata: %@\n ", Bytedata);

    //3:NSData 与 UIImage
    //获取Documents路径
    NSFileManager *fileManager=[NSFileManager defaultManager];
    
    //保存图片:UIImage-> NSData
    UIImage  *image=[UIImage imageNamed:@"1.jpg"];
    NSData   *imageData= UIImagePNGRepresentation(image);
    NSString *imagePath= [NSHomeDirectory() stringByAppendingPathComponent:@"520.png"];
    //NSLog(@"--->imagePath: %@",imagePath);
    [fileManager createFileAtPath:imagePath contents:imageData attributes:nil];
    
    //读取图片:NSData --> UIImage
    NSData      *readData=[NSData dataWithContentsOfFile:imagePath];
    UIImage     *dataImage= [UIImage imageWithData:readData];
    UIImageView *imageview= [[UIImageView alloc]initWithFrame:CGRectMake(60, 200, 200, 200)];
    imageview.image= dataImage;
    [self.view addSubview:imageview];
    NSLog(@"---<2.3>dataImage: %@\n ", dataImage);
    
    
    //4:NSData 与 MSMutableData
    //NSData --> MSMutableData
    NSData *ndata=[[NSData alloc]init];
    NSMutableData *mdata=[[NSMutableData alloc]init];
    mdata=(NSMutableData*)[NSData dataWithData:ndata];


    //5:NSData 合并为一个 NSMutableData
    //音频文件路径
    NSString *mp3Path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];
    NSString *mp3Path2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"mp3"];
    //音频数据
    NSData *sound1Data = [[NSData alloc] initWithContentsOfFile: mp3Path1];
    NSData *sound2Data = [[NSData alloc] initWithContentsOfFile: mp3Path2];
    //合并音频
    NSMutableData *sounds = [NSMutableData alloc];
    [sounds appendData:sound1Data];
    [sounds appendData:sound2Data];
    //保存音频
    NSLog(@"--->data length:%ld", [sounds length]);
    [sounds writeToFile:[self filePathWithName:@"tmp.mp3"] atomically:YES];

}
//filePathWithName
- (NSString *)filePathWithName:(NSString *)filename
{
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     return [documentsDirectory stringByAppendingPathComponent:filename];
}
//3.NSNumber
-(void)number
{
    /**
     *  NSNumber: 就是数字对象
     *  可以将基本数据类型包装起来,形成一个对象,这样就可以给其发送消息,装入NSArray中等等。
     * 
     * 在Objective-c中有int的数据类型,那为什么还要使用数字对象NSNumber?这是因为很多类(如NSArray)都要求使用对象,而int不是对象。
     *
     * NSNumber继承NSObject ,可以使用比较 compare: isEqual等消息
     * if([intNumber isEqualToNumber:floatNumber]) ....
     */
    NSNumber *intNumber,*floatNumber,*doubleNumber,*longNumber,*charNumber,*number1;
    intNumber   = [NSNumber numberWithInteger:123];         //1.创建integer类型对象
    longNumber  = [NSNumber numberWithLong:0xababab];       //2.创建long类型对象
    charNumber  = [NSNumber numberWithChar:'K'];            //3.创建char类型对象
    floatNumber = [NSNumber numberWithFloat:123.00];        //4.创建float类型对象
    doubleNumber= [NSNumber numberWithDouble:112233e+15];   //5.创建double类型对象

    //初始化实例
    number1 = [[NSNumber alloc] initWithInt:1000];
    NSLog(@"--->intValue: %d",[number1 intValue ]);//unsigned

    NSLog(@"--->intValue    : %i",[intNumber intValue]);
    NSLog(@"--->integerValue: %li",[intNumber integerValue]);
    NSLog(@"--->longValue   : %lx",[longNumber longValue]);
    NSLog(@"--->charValue   : %c",[charNumber charValue]);
    NSLog(@"--->floatValue  : %f",[floatNumber floatValue]);
    NSLog(@"--->doubleValue : %lg",[doubleNumber doubleValue]);
    //判断两个对象的值是否相等
    if ([intNumber isEqualToNumber:floatNumber] == YES ) {
        NSLog(@"值相等");
    } else {
        NSLog(@"值不相等");
    }
    /**
     *  NSComparisonResult
     *
     *  @param NSOrderedAscending  = -1 // 小于
     *  @param NSOrderedSame       = 0   // 等于
     *  @param NSOrderedDescending = 1   // 大于
     */
    //比较两个int值的大小
    if ( [intNumber compare:charNumber] == NSOrderedAscending) {
        NSLog(@"左边的数字大");
    } else {
        NSLog(@"右边的数字大");
    }
        
    float Amt = [@"1" floatValue];
    NSLog(@"---> Amt = %.2f",Amt);
    NSLog(@"---> Amt = %f",Amt);
    // float 类型比较大小
    NSNumber *a=[NSNumber numberWithFloat:Amt];
    NSNumber *b=[NSNumber numberWithFloat:0.01];
    if ([a compare:b]==NSOrderedDescending  || [a compare:b]==NSOrderedSame) {
        NSLog(@"a >= b"); //Amt >= 0.010000
    }

    // NSString 类型比较大小
    NSString *str1 = @"some text";
    if (str1!=nil && [str1 compare:@"some text"] == NSOrderedSame) {
        NSLog(@"---> Do something");
    }else {
        NSLog(@"---> Do something else");
    }
    
    /**
     *  那么当str1为空的时候,[str1 compare:@"some text"] 消息的返回就会为nil。nil表示一个空的Objective-C对象,实际就是表示一个空指针,而它代表的值就是0,与NSOrderedSame的值相等. 如此,回到最前面的if语句,如果str1为nil,那么整个语句的值为真。这会给程序造成非常严重的问题,小则逻辑错误,UI显示错误等,大则会造成数据泄漏等等。。。所以,一旦出现这种情况,还是很严重的。
     
       笔者个人建议,NSString类型比较时,要判断非空nil
     */
}

你可能感兴趣的:(iOS-常量与变量)