zoukankan      html  css  js  c++  java
  • OC NSNumber和NSValue和NSDate和NSData

    一 NSNumber

     1 //
     2 //  main.m
     3 //  07-NSNumber
     4 //
     5 //  Created by apple on 13-8-12.
     6 //  Copyright (c) 2013年 itcast. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 int main()
    12 {
    13     // @20  将 20包装成一个NSNumber对像
    14     
    15     
    16     NSArray *array = @[
    17     
    18     @{@"name" : @"jack", @"age" : @20},
    19     
    20     @{@"name" : @"rose", @"age" : @25},
    21 22 @{@"name" : @"jim", @"age" : @27} 23 ]; 24 25 26 // 将各种基本数据类型包装成NSNumber对象 27 @10.5; 28 @YES; 29 @'A'; // NSNumber对象 30 31 @"A"; // NSString对象 32 33 34 35 // 将age变量包装成NSNumber对象 36 int age = 100; 37 @(age); 38 //[NSNumber numberWithInt:age]; 39 40 41 NSNumber *n = [NSNumber numberWithDouble:10.5]; 42 43 44 int d = [n doubleValue]; 45 46 47 48 int a = 20; 49 50 // @"20" 51 NSString *str = [NSString stringWithFormat:@"%d", a]; 52 NSLog(@"%d", [str intValue]); 53 54 return 0; 55 } 56 57 void test() 58 { 59 NSNumber *num = [NSNumber numberWithInt:10]; 60 61 NSDictionary *dict = @{ 62 @"name" : @"jack", 63 64 65 @"age" : num 66 67 }; 68 69 NSNumber *num2 = dict[@"age"]; 70 71 72 int a = [num2 intValue]; 73 74 NSLog(@"%d" , a); 75 }

    二 NSValue

     1 #import <Foundation/Foundation.h>
     2 
     3 
     4 // NSNumber之所以能包装基本数据类型为对象,是因为继承了NSValue
     5 
     6 int main()
     7 {
     8     
     9     // 结构体--->OC对象
    10     
    11     CGPoint p = CGPointMake(10, 10);
    12     // 将结构体转为Value对象
    13     NSValue *value = [NSValue valueWithPoint:p];
    14     
    15     // 将value转为对应的结构体
    16     // [value pointValue];
    17     
    18     NSArray *array = @[value ];
    19     
    20     
    21         // insert code here...
    22     // NSLog(@"这是哥修改过的东西6");
    23     return 0;
    24 }

    三 NSDate

     1 #import <Foundation/Foundation.h>
     2 void date2string();
     3 void use();
     4 int main()
     5 {
     6     // 09/10/2011
     7     NSString *time = @"2011/09/10 18:56";
     8     
     9     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    10     formatter.dateFormat = @"yyyy/MM/dd HH:mm";
    11     
    12     NSDate *date = [formatter dateFromString:time];
    13     NSLog(@"%@", date);
    14     date2string();
    15     use();
    16     return 0;
    17 }
    18 
    19 
    20 void date2string()
    21 {
    22     NSDate *date = [NSDate date];
    23     
    24     
    25     // 日期格式化类
    26     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    27     
    28     // y 年  M 月  d 日
    29     // m 分 s 秒  H (24)时  h(12)时
    30     formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    31     
    32     NSString *str = [formatter stringFromDate:date];
    33     
    34     NSLog(@"%@", str);
    35 }
    36 
    37 void use()
    38 {
    39     // 创建一个时间对象
    40     NSDate *date = [NSDate date];
    41     // 打印出的时候是0时区的时间(北京-东8区)
    42     NSLog(@"%@", date);
    43     
    44     NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];
    45     
    46     
    47     // 从1970开始走过的秒数
    48     NSTimeInterval seconds = [date2 timeIntervalSince1970];
    49     
    50     // [date2 timeIntervalSinceNow];
    51 }

    四、NSNumber、NSValue、NSData和NSDate课堂笔记

      1 #import <Foundation/Foundation.h>
      2 
      3 #import "People.h"
      4 
      5 typedef struct student
      6 {
      7     char name[10];
      8     NSInteger age;
      9 }stu;
     10 
     11 int main(int argc, const char * argv[]) {
     12     @autoreleasepool {
     13         // insert code here...
     14         // NSLog(@"Hello, World!");
     15         
     16         /*
     17          0.炒鸡排序
     18          1.NSNumber -对基本数据类型进行对象转换
     19          2.NSValue  -对结构体或自定义结构体转换
     20          3.NSData   -对字符串进行封装(二进制转换)
     21          4.NSDate   -日期
     22          {
     23             a.日期初始化
     24             b.日期格式使用(NSDateFormatter)
     25             c.时间间隔使用(NSTimeinterval)
     26             d.验证快速遍历和for循环
     27          
     28          }
     29          */
     30         
     31         
     32         //NSNumber
     33         
     34         int number = 18;
     35         
     36         int number1 = 19;
     37         //基本数据类型包装成对象
     38         NSNumber *num = [[NSNumber alloc] initWithInt:number1];
     39         
     40         
     41         
     42         NSArray *arr1 = @[num,@(number)];
     43         NSLog(@"%@",arr1);
     44        // NSLog(@"%d",num);
     45         
     46         // 把对象还原成基本数据类型
     47         int newNumber1 = [[arr1 objectAtIndex:0] intValue];
     48         NSLog(@"%d",newNumber1);
     49         
     50         
     51         
     52         //NSValue
     53         NSRange range = NSMakeRange(10, 6);
     54         //把结构体包装成对象
     55         NSValue *rangeValue = [NSValue valueWithRange:range];
     56 
     57         NSArray *arr2 = @[rangeValue];
     58         
     59         NSLog(@"%lu %lu",range.location,range.length);
     60         NSLog(@"%@",arr2);
     61         
     62         NSRange newRange;
     63         //把对象还原成结构体
     64         [rangeValue getValue:&newRange];
     65         NSLog(@"%lu %lu",newRange.location,newRange.length);
     66         
     67         //封装自定义结构体
     68         stu bowen;
     69         bowen.age = 20;
     70         // 把自定义结构体封装成对象
     71         NSValue *stuValue = [[NSValue alloc] initWithBytes:&bowen
     72                                                   objCType:@encode(stu)] ;
     73         NSLog(@"%@",stuValue);
     74         
     75         stu newbowen;
     76         //  把对象还原成结构体
     77         [stuValue getValue:&newbowen];
     78         NSLog(@"%ld",newbowen.age);
     79         
     80         
     81         //NSData
     82         NSString *str = @"123";
     83         // 把字符串封装成Data对象
     84         NSData *strData = [str dataUsingEncoding:NSUTF8StringEncoding];
     85         NSLog(@"%@",strData);
     86         // 把Data还原成字符串
     87         NSString *finalStr = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
     88         NSLog(@"%@",finalStr);
     89         
     90         
     91         //NSDate
     92         // 日期初始化
     93         NSDate *now = [NSDate date];
     94         NSLog(@"%@",now);
     95         
     96         NSDate *date1 =[NSDate dateWithTimeInterval:60*60*8 sinceDate:now];
     97         NSLog(@"%@",date1);
     98         
     99         NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:60*60*24*365*30];
    100         NSLog(@"%@",date2);
    101         
    102         NSDate *date3 = [NSDate dateWithTimeIntervalSinceNow:60*60*1];
    103         NSLog(@"%@",date3);
    104         
    105         NSDate *date4 = [NSDate dateWithTimeIntervalSinceReferenceDate:60*60];
    106         NSLog(@"%@",date4);
    107         
    108         //日期格式NSDateFormatter
    109         NSDate *date5 = [NSDate date];
    110         
    111         NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    112         
    113         /*
    114          YYYY/yyyy 年
    115          MM        月
    116          dd        日
    117          
    118          HH/hh     时
    119          mm        分
    120          ss        秒
    121          */
    122         //日期转换成字符串
    123         [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    124         NSString *dateStr = [formatter stringFromDate:date5];
    125         NSLog(@"%@",dateStr);
    126         
    127         //字符串转化成日期
    128         NSString *newStr = @"2015-08-18 23-17-19";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    129 NSDate *newDate = [dateformatter dateFromString:newStr]; 130 NSLog(@"%@",newDate); 131 132 133 134 //时间间隔 NSTimeinterval 135 NSDate *date6 = [NSDate date]; 136 NSDate *date7 = [NSDate dateWithTimeIntervalSinceNow:60]; 137 NSTimeInterval timeinterval = [date6 timeIntervalSinceDate:date7]; 138 139 NSLog(@"%f",timeinterval); 140 141 142 143 144 145 146 //快速遍历 147 NSArray *arr10 =@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"]; 148 NSDate *before = [NSDate date]; 149 150 for (NSString *str10 in arr10) { 151 NSLog(@"%@",str10); 152 } 153 154 NSDate *after =[NSDate date]; 155 NSTimeInterval timeinterval1 = [after timeIntervalSinceDate:before]; 156 157 NSLog(@"快速遍历:%f",timeinterval1); 158 159 //普通循环 160 NSDate *before1 = [NSDate date]; 161 162 for (int i=0; i<arr10.count; i++) { 163 NSLog(@"%@",[arr10 objectAtIndex:i]); 164 } 165 166 NSDate *after1 = [NSDate date]; 167 NSTimeInterval timeinterval2 = [after1 timeIntervalSinceDate:before1]; 168 169 NSLog(@"普通循环:%f",timeinterval2); 170 171 } 172 return 0; 173 }
  • 相关阅读:
    年轻人绝对不懂的人际关系经验
    MRCPv2在电信智能语音识别业务中的应用
    S3 介绍
    RGW 学习 前言
    CEPH 自动化测试用例介绍
    CentOS7使用yum安装ceph rpm包
    Placement_pools on Rados-GW
    ceph log机制
    bucket list 函数解析
    ceph 初始化函数解析
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5044896.html
Copyright © 2011-2022 走看看