OC各种数据类型之间的转换方法
Posted 星星太阳鱼虾蟹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OC各种数据类型之间的转换方法相关的知识,希望对你有一定的参考价值。
NSNumber转NSString:
假设现有一NSNumber的变量A,要转换成NSString类型的B
方法如下:
NSNumberFormatter* numberFormatter = [[NSNumberFormatteralloc] 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]);
- }
以上是关于OC各种数据类型之间的转换方法的主要内容,如果未能解决你的问题,请参考以下文章