1.NSNumber

将基础数类型数据转成对象数据(比如int  float double BOOL  long等等)

//通过NSNumber将基础数类型数据转成对象数据。

NSNumber * intNumber = [[NSNumber alloc] initWithInt:50];

NSNumber * floatNumber = [NSNumber numberWithFloat:45.3];

//xcode4.6以后,可以采用如下写法

NSNumber * doubleNumber = @3.14;

NSNumber * boolNumber2 = @YES;

//从对象中取出基础类型数据

double d = doubleNumber.doubleValue;

float f = floatNumber.floatValue;

2. NSValue

NSValue是NSNumber的父类,可以存储任何类型的数据,包括复合数据类型(数组,指针,结构体等).

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

//第一个参数传第一个字节的地址,无所谓类型。

//第二个参数决定数据的大小。

//@encode将类型转成NSValue可以识别的字符串。

NSValue * value = [NSValue valueWithBytes:a objCType:@encode(int[10])];

//取出数据

int b[10];

[value getValue:b];

typedef struct

{

int id;

float height;

unsigned char flag;

}Test;

Test test1;

test1.id = 1;

test1.height = 23.5;

test1.flag = 'A';

NSValue * test = [NSValue valueWithBytes:&test1 objCType:@encode(Test)];

//声明test2,为了得到test1的值

Test test2;

//把test的值赋值到test2中

[test getValue:&test2];

3. NSNull

如要确实需要存储一个表示“什么都没有”的值,可以使用NSNull类。

NSNull值为空的对象,可以加入到数组或者词典中 [NSNull null]; 创建表示空的对象

nil -> Null-pointer to objective- c object  ———对象指针为空

Nil -> Null-pointer to objective- c class  ———类指针为空

NULL-> null pointer to primitive type or absence of data.  ———基本类型指针为空

空指针不能用来加入到数组或字典,所以可以采用[NSNull null]的方式加入空对象。

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

[dict setObject:[NSNull null] forKey:@"someKey"];

//nil 被用来作为集合结束的标志,不能填入到array和dictionary中

//[dict setObject:nil forKey:@"wrongKey”];———错误的

=============================================

课堂练习:

<1> 创建一个数组,穿插存入空对象和字符串

<2>   写一个结构体,存成NSValue,再取出来

===============================

4. NSDate

//创建当前时间,以格林尼治时间为准

NSDate * date = [NSDate date];

NSLog(@"%@", date);

//某一时间过了多少秒,创建一个新的时间点

NSDate * date2 = [NSDate dateWithTimeInterval:3600 sinceDate:date];

NSLog(@"%@", date2);

//从当前时间过了多少秒,生成新的时间点

NSDate * date3 = [NSDate dateWithTimeIntervalSinceNow:-3600];

NSLog(@"%@", date3);

//从1970/01/01 0时 GMT为准,过后多少秒,生成新的时间

NSDate * date4 = [NSDate dateWithTimeIntervalSince1970:3600];

NSLog(@"%@", date4);

//未来时间,用于暂停定时器,将定时器启动时间设为遥远的未来 Never

NSDate * futureDate = [NSDate distantFuture];

//过去时间,用于重启定时器,将定时器启动时间设为遥远的过去 ever

NSDate * pastDate = [NSDate distantPast];

NSLog(@"%@",futureDate);

NSLog(@"%@",pastDate);

//创建时间戳

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

//使用时间戳的目的,是为了自定义时间的打印格式

dateFormatter.dateFormat = @"yyyy年MM月dd日EEE HH:mm:ss.S";

dateFormatter.dateFormat = @"yyyy-MM-dd ahh时mm分ss秒";

//HH是24小时制,hh是12小时制  a表示上下午

//EEE表示周几  EEEE表示星期几

NSLog(@"%@", [dateFormatter stringFromDate:date]);

NSDate *da = [dateFormatter dateFromString:[dateFormatter stringFromDate:date]];

NSLog(@"%@",da);

NSDate

//得到当前的日期
 NSDate *date = [NSDate date];
 NSLog(@"date:%@",date);
 
 //得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow:
 NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)];
 NSLog(@"yesterday:%@",yesterday);

NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
 NSDate *date = [NSDate date];
 [formatter setTimeStyle:NSDateFormatterMediumStyle];
 NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
 NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
 NSInteger unitFlags = NSYearCalendarUnit | 
                       NSMonthCalendarUnit |
                       NSDayCalendarUnit | 
                       NSWeekdayCalendarUnit | 
                       NSHourCalendarUnit |
                       NSMinuteCalendarUnit |
                       NSSecondCalendarUnit;
 //int week=0;
 comps = [calendar components:unitFlags fromDate:date];
 int week = [comps weekday]; 
 int year=[comps year]; 
 int month = [comps month];
 int day = [comps day];
 //[formatter setDateStyle:NSDateFormatterMediumStyle];
 //This sets the label with the updated time.
 int hour = [comps hour];
 int min = [comps minute];
 int sec = [comps second];
 NSLog(@"week%d",week);
 NSLog(@"year%d",year);
 NSLog(@"month%d",month);
 NSLog(@"day%d",day);
 NSLog(@"hour%d",hour);
 NSLog(@"min%d",min);
 NSLog(@"sec%d",sec);

//得到毫秒
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
 [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
 //[dateFormatter setDateFormat:@"hh:mm:ss"]
 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
 NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);
 [dateFormatter release];

 
 /*----日期时间的比较----*/
// 当前时间
NSDate *currentDate = [NSDate date];
// 比当前时间晚一个小时的时间
NSDate *laterDate = [[NSDate alloc] initWithTimeInterval:60*60 sinceDate:[NSDate date]];
// 比当前时间早一个小时的时间
NSDate *earlierDate = [[NSDate alloc] initWithTimeInterval:-60*60 sinceDate:[NSDate date]];
// 比较哪个时间迟
if ([currentDate laterDate:laterDate]) {
   // 打印结果:current-2013-08-16 09:25:54 +0000比later-2013-08-16 10:25:54 +0000晚
  NSLog(@"current-%@比later-%@晚",currentDate,laterDate);
}
// 比较哪个时间早
if ([currentDate earlierDate:earlierDate]) {
  // 打印结果:current-2013-08-16 09:25:54 +0000 比 earlier-2013-08-16 08:25:54 +0000
  NSLog(@"current-%@ 比 earlier-%@ 早",currentDate,earlierDate);
}
 
if ([currentDate compare:earlierDate]==NSOrderedDescending) {
  // 打印结果
NSLog(@"current 晚");
}
 
if ([currentDate compare:currentDate]==NSOrderedSame) {
  // 打印结果
  NSLog(@"时间相等");
}
 
if ([currentDate compare:laterDate]==NSOrderedAscending) {
// 打印结果
  NSLog(@"current 早");
}

Foundation框架下的常用类(NSNumber, NSValue, NSDate,NSDateFormatter)的更多相关文章

  1. Foundation框架下的常用类:NSNumber、NSDate、NSCalendar、NSDateFormatter、NSNull、NSKeyedArchiver

    ========================== Foundation框架下的常用类 ========================== 一.[NSNumber] [注]像int.float.c ...

  2. (转载)OC学习篇之---Foundation框架中的其他类(NSNumber,NSDate,NSExcetion)

    前一篇说到了Foundation框架中的NSDirctionary类,这一一篇来看一下Foundation的其他常用的类:NSNumber,NSDate,NSException. 注:其实按照Java ...

  3. OC学习篇之---Foundation框架中的其他类(NSNumber,NSDate,NSExcetion)

    1.NSNumber 这个类主要是用来封装基本类型的,说到这里,就不得不说一下了: OC中的集合是不允许存入基本类型的,所以NSNumber类就诞生了,需要将基本类型封装一下,然后存进去,这个类似于J ...

  4. (转载)OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类

    在之前的一篇文章中介绍了Foundation框架中的NSString类和NSMutableString类,今天我们继续来看一下Foundation框架中的NSArray类和NSMutableArray ...

  5. OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类

    我们继续来看一下Foundation框架中的NSArray类和NSMutableArray类,其实NSArray类和Java中的List差不多,算是一种数据结构,当然我们从这两个类可以看到,NSArr ...

  6. 黑马程序员_ Objective-c 之Foundation之NSNumber ,NSValue, NSDate

    Objective-c 之Foundation之NSNumber ,NSValue, NSDate 1.NSNumber具体用法如下: 在Objective-c中有int的数据类型,那为什么还要使用数 ...

  7. (转载)OC学习篇之---Foundation框架中的NSDirctionary类以及NSMutableDirctionary类

    昨天学习了Foundation框架中NSArray类和NSMutableArray类,今天来看一下Foundation框架中的NSDirctionary类,NSMutableDirctionary类, ...

  8. OC学习篇之---Foundation框架中的NSDictionary类以及NSMutableDictionary类

    今天来看一下Foundation框架中的NSDictionary类,NSMutableDictionary类,这个和Java中的Map类很想,OC中叫字典,Java中叫Map,还有字典是无序的,这个和 ...

  9. OC学习篇之---Foundation框架中的NSDirctionary类以及NSMutableDirctionary类

    昨天学习了Foundation框架中NSArray类和NSMutableArray类:http://blog.csdn.net/jiangwei0910410003/article/details/4 ...

随机推荐

  1. [BZOJ3453]tyvj 1858 XLkxc:拉格朗日插值

    分析 之前一直不知道拉格朗日插值是干什么用的,只会做模板题,做了这道题才明白这个神奇算法的用法. 由题意可知,\(f(x)\)是关于\(x\)的\(k+1\)次函数,\(g(x)\)是关于\(x\)的 ...

  2. ELK日志平台搭建

    功能: 1. 查看当天的服务器日志信息(要求:在出现警告甚至警告级别以上的都要查询)2. 能够查看服务器的所有用户的操作日志3. 能够查询nginx服务器采集的日志(kibana作图)4. 查看tom ...

  3. tomcat8.5部署管理控制台

    1.修改 conf/tomcat-users.xml 文件 <role rolename="manager-gui"/> <role rolename=" ...

  4. Oracle JET Model 数据获取与使用

    Oracle JET 应用程序数据可以来自生成 JSON 数据的任何 Web 数据源,例如 REST 服务,服务器发送事件(SSE)或 WebSocket .此外,Oracle JET 还提供了基于 ...

  5. Promethus

    https://blog.csdn.net/zl1zl2zl3/article/details/74332437

  6. qemu-kvm: unable to map backing store for guest RAM: Cannot allocate memory

    当给 KVM 虚拟机设置 hugepage 时,需要在虚拟机的配置文件里加上下面一段: <memoryBacking> <hugepages/></memoryBacki ...

  7. 常量const实践

    这篇文章写的很好:https://www.cnblogs.com/zhangfeionline/p/5882790.html 自己是实践: 1. 定义时必须初始化值,不然如下错误: 2. 3. 使用:

  8. 十一、RF操作滚动条

    两种方式: 方式一:window.scrollBy(0, document.body.scrollHeight) 方式二:window.scrollTo(0, document.body.scroll ...

  9. 2018.03.29 python-pandas 数据读取

    #数据读取# read_table,read_csv,read_excel #读取普通分隔数据:read_table #可以读取txt,csv import os import pandas as p ...

  10. Dubbo分布式服务框架入门(附工程)

    要想了解Dubbo是什么,我们不防先了解它有什么用. 使用场景:比如我想开发一个网上商城项目,这个网上商城呢,比较复杂,分为pc端web管理后台,微信端销售公众号,那么我们分成四个项目,pc端网站,微 ...