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. Spring Boot教程(十)异步方法测试

    测试 测试代码如下: @Component public class AppRunner implements CommandLineRunner { private static final Log ...

  2. Hive函数介绍

    一些函数不太会,查了些资料,分享一下 Hive已定义函数介绍: 1.字符串长度函数:length 语法: length(string A)返回值: int举例:[sql] view plain cop ...

  3. Starting MySQL... ERROR! The server quit without updating PID file (/usr/local/mysql/data/VM_0_6_centos.pid)

    刚接触MySql数据库,参考一些文章后搭建起来了也创建了数据库,程序跑到很好,一觉醒来突然连接不上了 MySql数据库了. 研究了好一会才找到原因. 现象: 登录数据库失败 [root@VM_0_6_ ...

  4. 《Effective Java》读书笔记 - 2.创建和销毁对象

    Chapter 2 Creating and Destroying Objects item 1:Consider static factory methods instead of construc ...

  5. LibUsbDotNet使用方法

    最近在用C#调试USB程序,libusb源码是C语言的,C#用起来不方便,偶然在网上看到了LibUsbDotNet,这是开源的项目,下载后参考Example,用起来非常方便. LibUsbDotNet ...

  6. 惠普DL360G6安装ESXi主机后设置多块网卡

    需要先把服务器的网线连接到路由器 然后打开esxi设置网络的netwoork adapters 选中多块网卡,点确定保持 然后在到esxi客户端操作: 直接下一步 这里填上路由器分配的网段ip即可了

  7. 【重点突破】—— UniApp微信小程序开发教程学习Three

    一.实战 HBuilderX:在微信小程序中运行页面,需要设置->安全 开启微信小程序服务端口,HBuilder工具->设置->配置程序路径 网络请求.模板语法.打开页面.页面传参 ...

  8. 转]@SuppressWarnings 详解

    转自:http://gladto.iteye.com/blog/728634 背景知识:        从JDK5开始提供名为Annotation(注释)的功能,它被定义为JSR-175规范.注释是以 ...

  9. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_7 Mybatis中参数的深入-使用实体类的包装对象作为查询条件

    pojo对象就是实体类 综合查询-实体类包装起来做查询 新建实体类QueryVo 提供一个User对象属性,并生成getter和setter 测试 修改dao接口中的返回类型为List<User ...

  10. Unity3D 协程 Coroutine

    协程(Coroutine)的概念存在于很多编程语言,例如Lua.ruby等.而由于Unity3D是单线程的,因此它同样实现了协程机制来实现一些类似于多线程的功能,但是要明确一点协程不是进程或线程,其执 ...