1: NSString <==> NSInteger
NSNumber *indexNum = [NSNumber numberWithInt:index];
UITextView *login = (UITextView*)[[UIView alloc] viewWithTag:1]; NSNumberFormatter *formater = [[NSNumberFormatter alloc ] init]; [login setText:[formater stringFromNumber:indexNum]];
1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用 NSInteger,因为这样就不用考虑设备是32位的还是64位的 。
2. NSUInteger是无符号的 ,即没有负数,NSInteger是有符号的。
3.有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
NSInteger是基础类型,但是NSNumber是一个类 。如果想要在NSMutableArray里存储一个数值,直接用NSInteger是不行的,比如在一个NSMutableArray里面这样用:
- NSMutableArray *array = [[NSMutableArray alloc]init];
- [array addObject:[ NSNumber numberWithInt :88]];
NSString *teststring = @"32.54" ;
int i = [teststring intValue ];
float i1 = [teststring floatValue ];
double i2 = [teststring doubleValue ];
NSInteger i3 = [teststring integerValue ];
NSString *tostring = nil ;
tostring = [ NSString stringWithFormat: @"%d" ,i];
NSLog ( @"tostring-int:%@",tostring);
tostring = [ NSString stringWithFormat: @"%.3f" ,i1];
NSLog ( @"tostring-float:%@",tostring);
tostring = [ NSString stringWithFormat: @"%e" ,i2];
NSLog ( @"tostring-double:%@",tostring);
NSNumber转NSString:
假设现有一NSNumber的变量A,要转换成NSString类型的B
方法如下:
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc ] init ];
B = [numberFormatter stringFromNumber :A ];
[numberFormatter release ];
nsstring和float 还有int之间的转换
NSString *tempA = @"123";
NSString *tempB = @"456";
1,字符串拼接
NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];
2,字符转int
int intString = [newString intValue];
3,int转字符
NSString *stringInt = [NSString stringWithFormat:@"%d",intString];
4,字符转float
float floatString = [ newString floatValue];
5,float转字符
NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];
///- NSString *a = [[NSString alloc] initWithString : @"5, 10, 2, 0, 0, 0.01, 1, 0.05, 0, 0, 0"];
- NSArray *bullteData = [a componentsSeparatedByString:@","];
- [a release];
- for (int i = 0; i < [bullteData count]; i++) {
- NSLog(@"%f",[[NSString stringWithFormat:@"%@",[bullteData objectAtIndex:i]] floatValue]);
- }
- NSString *a = [[NSString alloc] initWithString : @"5, 10, 2, 0, 0, 0.01, 1, 0.05, 0, 0, 0"];
- NSArray *bullteData = [a componentsSeparatedByString:@","];
- [a release];
- for (int i = 0; i < [bullteData count]; i++) {
- NSLog(@"%f",[[NSString stringWithFormat:@"%@",[bullteData objectAtIndex:i]] floatValue]);
- }