iOS类型对比

    //字符串转int double bool
    NSString *string = [NSString stringWithFormat:@"1000是可敬的坑"];
    int intStr = [string intValue];//1000
    double doubleStr = [string doubleValue];//1000.000000
    BOOL boolStr = [string boolValue];//yes(1)

    NSLog(@"string:%@-intStr:%d-doubleStr:%f-boolStr:%d",string,intStr,doubleStr,boolStr);
    //输出结果:string:1000是可敬的坑-intStr:1000-doubleStr:1000.000000-boolStr:1

    //int double bool转字符串
    int data = 100;
    double doubled = 100.000000;
    BOOL boolb = YES;

    NSString *strInt = [NSString stringWithFormat:@"%d",data];//100
    NSString *strD = [NSString stringWithFormat:@"%f",doubled];//100.000000
    NSString *strB = [NSString stringWithFormat:@"%d",boolb];//1

    NSLog(@"data:%d/doubled:%f/boolb:%d\n strInt:%@/strD:%@/strB:%@",data,doubled,boolb,strInt,strD,strB);
    //输出结果:data:100/doubled:100.000000/boolb:1/strInt:100/strD:100.000000/strB:1

    //同类型对比
    NSLog(@"%@",data == intStr ? @"yes" : @"no");//no
    NSLog(@"%@",doubled == doubleStr ? @"yes" : @"no");//no
    NSLog(@"%@",boolb == boolStr ? @"yes" : @"no");//yes

    //不同类型对比
    NSLog(@"%@",data == doubled ? @"yes" : @"no");//yes
    NSLog(@"%@",doubled == boolb ? @"yes" : @"no");//no
    NSLog(@"%@",data == boolb ? @"yes" : @"no");//no

    //字符串对比
    //对比是否相同
    NSString *str1 = @"11";
    NSLog(@"%@",[str1 isEqualToString:@"11"] ? @"yes" : @"no");//yes

    //比较大小
    /**NSComparisonResult枚举类型
    type enum _NSComparisonResult{
        NSOrderedAscending = -1,//递增
        NSOrderedSame,//相同
        NSOrderedDescending//递减
    }*/
    int com = [str1 compare:@"111"];//递增
    NSString *str2 = @"11";
    NSString *str3 = @"1";
    int com2 = [str1 compare:str2];//??
    int com3 = [str1 compare:str3];//??
    switch (com) {
        case NSOrderedAscending:
            NSLog(@"递增");
            break;
        case NSOrderedSame:
            NSLog(@"相同");
            break;
        case NSOrderedDescending:
            NSLog(@"递减");
            break;
        default:
            break;
    }

NSComparisonResult枚举类型

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值