//字符串转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;
}
iOS类型对比
最新推荐文章于 2023-05-15 21:45:24 发布