1、字符串类型转换为其他类型
@property (readonly) double doubleValue;
@property (readonly) float floatValue;
@property (readonly) int intValue;
@property (readonly) NSInteger integerValue
@property (readonly) long long longLongValue
@property (readonly) BOOL boolValue
NSString *str = @"12.2";
NSLog(@"str = %@",str);
double strDou = str.doubleValue;
NSLog(@"strDou = %f",strDou);
2、将基本数据类型转成 OC 对象(NSNumber)
- 1、NSNumber:是 Foundation 框架中的一个类。
- 2、这个类型的对象的作用就是用来包装基本数据类型的。
NSNumber *num1 = [NSNumber numberWithFloat:10.1f];
NSNumber *num2 = [NSNumber numberWithFloat:10.2f];
NSNumber *num3 = [NSNumber numberWithFloat:10.3f];
NSArray *arr = @[num1, num2, num3];
for (NSNumber *num in arr) {
NSLog(@"num = %f",num.floatValue);
}
NSNumber的简写: @10, @20…如果是变量的话,就要将变量用小括号括起来就好了
NSNumber *num1 = @10;
int age = 22;
NSNumber *num2 = @(age);
NSLog(@"num1 = %@,num2 = %@",num1,num2);
|