zoukankan      html  css  js  c++  java
  • float类型转对象 对象转float类型(一)

    //float类型转化为对象
    CGFloat fValue = 1.f;
    NSNumber *objNo = [NSNumber numberWithFloat:fValue];
    数值、BOOL型都可以转成Number。获取的时候,再转成数字
    //对象转化为数字
    CGFloat fValue1 = [objNo floatValue];


    IOS代码
    1. NSString *tempA = @"123";  
    2. NSString *tempB = @"456";  

    1,字符串拼接 

    1. NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];  

    2,字符转int 

    1. int intString = [newString intValue];  

    3,int转字符 
     
    1. NSString *stringInt = [NSString stringWithFormat:@"%d",intString];  

    4,字符转float 
     
    1. float floatString = [newString floatValue];  

    5,float转字符 

    1. NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];  
     
     
    二、常用数据类型
     
     

    一、time_t介绍

    包含文件:
    #ifndef __TIME_T
    #define __TIME_T          /* 避免重复定义 time_t */
    typedef long     time_t;    /* 时间值time_t 为长整型的别名*/
    #endif
    既然time_t实际上是长整型,到未来的某一天,从一个时间点(一般是1970年1月1日0时0分0秒)到那时的秒数(即日历时间)超出了长整形所能表示的数的范围怎么办?对time_t数据类型的值来说,它所表示的时间不能晚于2038年1月18日19时14分07秒。为了能够表示更久远的时间,一些编译器厂商引入了64位甚至更长的整形数来保存日历时间。比如微软在Visual C++中采用了__time64_t数据类型来保存日历时间,并通过_time64()函数来获得日历时间(而不是通过使用32位字的time()函数),这样就可以通过该数据类型保存3001年1月1日0时0分0秒(不包括该时间点)之前的时间。
    在time.h头文件中,我们还可以看到一些函数,它们都是以time_t为参数类型或返回值类型的函数:
     
    double difftime(time_t time1, time_t time0);
    time_t mktime(struct tm * timeptr);
    time_t time(time_t * timer);
    char * asctime(const struct tm * timeptr);
    char * ctime(const time_t *timer);
     
    此外,time.h还提供了两种不同的函数将日历时间(一个用time_t表示的整数)转换为我们平时看到的把年月日时分秒分开显示的时间格式tm:
     
    struct tm * gmtime(const time_t *timer);                                         
    struct tm * localtime(const time_t * timer);
    二、time_t转换为NSString

        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

     
     
    1,NSData 与 NSString
      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];

     
    3,NSData 与 UIImage
      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);
     
    4,NSData 与 NSMutableData
      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;
  • 相关阅读:
    用Python完成一个汇率转换器
    鸿蒙如何用JS开发智能手表App
    鸿蒙如何用JS开发智能手表App
    SAP Spartacus SplitViewComponent Migration 的一个具体例子
    SAP Spartacus B2B 页面 Popover Component 的条件显示逻辑
    SAP Spartacus 升级时关于 schematics 的更新
    SAP Spartacus B2B 页面 Disable 按钮的显示原理
    SAP Spartacus B2B 页面 Disable Confirmation 对话框的显示原理
    通过 Feature Level 动态控制 SAP Spartacus 的页面显示
    SAP Commerce Cloud Build Manifest Components
  • 原文地址:https://www.cnblogs.com/ithongjie/p/5070918.html
Copyright © 2011-2022 走看看