float类型转对象 对象转float类型(一)
//float类型转化为对象
CGFloat fValue = 1.f;
NSNumber *objNo = [NSNumber numberWithFloat:fValue];
数值、BOOL型都可以转成Number。获取的时候,再转成数字
//对象转化为数字
CGFloat fValue1 = [objNo floatValue];
- 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];
一、time_t介绍
1)转换方法如下:
-(NSString *)dateInFormat:(time_t)dateTime format:(NSString*) stringFormat
{
char buffer[80];
const char *format = [stringFormat UTF8String];
struct tm * timeinfo;
timeinfo = localtime(&dateTime);
strftime(buffer, 80, format, timeinfo);
return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}
2)如何使用
在需要转换的文件中,进行如下操作:
//时间格式
NSString *str = @"%d.%m.%Y %H:%M:%S";
//其中sts.createdAt为time_t类型,这个数据是来自新浪微博
NSString *time = [self dateInFormat:sts.createdAt format:str];
NSLog(@"createdAt: %@",time);
控制台输出如下:
createdAt: 05.08.2011 00:17:56
NSData --> NSString
NSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding];
NSString --> NSData
NSString *aString = @"1234";
NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
2,NSData 与 Byte
NSData --> Byte
NSString *testString = @"1234567890";
NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
Byte *testByte = (Byte *)[testData bytes];
Byte --> NSData
Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
NSData *adata = [[NSData alloc] initWithBytes:byte length:24];
NSData --> UIImage
UIImage *aimage = [UIImage imageWithData: imageData];
//例:从本地文件沙盒中取图片并转换为NSData
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *name = [NSString stringWithFormat:@"ceshi.png"];
NSString *finalPath = [path stringByAppendingPathComponent:name];
NSData *imageData = [NSData dataWithContentsOfFile: finalPath];
UIImage *aimage = [UIImage imageWithData: imageData];
UIImage-> NSData
NSData *imageData = UIImagePNGRepresentation(aimae);
NSData --> MSMutableData
NSData *data=[[NSData alloc]init];
NSMutableData *mdata=[[NSMutableData alloc]init];
mdata=[NSData dataWithData:data];
5,NSData合并为一个NSMutableData
1 - (NSString *)filePathWithName:(NSString *)filename 2 { 3 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 4 NSString *documentsDirectory = [paths objectAtIndex:0]; 5 return [documentsDirectory stringByAppendingPathComponent:filename]; 6 } 7 8 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 9 //音频文件路径 10 NSString *mp3Path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"]; 11 NSString *mp3Path2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"mp3"]; 12 //音频数据 13 NSData *sound1Data = [[NSData alloc] initWithContentsOfFile: mp3Path1]; 14 NSData *sound2Data = [[NSData alloc] initWithContentsOfFile: mp3Path2]; 15 //合并音频 16 NSMutableData *sounds = [NSMutableData alloc]; 17 [sounds appendData:sound1Data]; 18 [sounds appendData:sound2Data]; 19 //保存音频 20 21 NSLog(@"data length:%d", [sounds length]); 22 23 [sounds writeToFile:[self filePathWithName:@"tmp.mp3"] atomically:YES]; 24 25 [window makeKeyAndVisible]; 26 27 return YES; 28 }
1、当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2、NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
3、注意:既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
NSInteger是基础类型,但是NSNumber是一个类。如果想要存储一个数值,直接用NSInteger是不行的,比如在一个Array里面这样用:
NSArray *array= [[NSArray alloc]init]; [array addObject:3];//会编译错误
这样是会引发编译错误的,因为NSArray里面放的需要是一个类,但‘3’不是。这个时候需要用到NSNumber:
NSArray *array= [[NSArray alloc]init]; [array addObject:[NSNumber numberWithInt:3]];
Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型。
例如以下创建方法:
+ (NSNumber*)numberWithChar: (char)value; + (NSNumber*)numberWithInt: (int)value; + (NSNumber*)numberWithFloat: (float)value; + (NSNumber*)numberWithBool: (BOOL) value;
将基本类型数据封装到NSNumber中后,就可以通过下面的实例方法重新获取它:
- (char)charValue; - (int)intValue; - (float)floatValue; - (BOOL)boolValue; - (NSString*)stringValue;