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. VS Code报错Module 'xx' has no 'xx' member pylint(no-member)解决办法

    pylint是vscode的python语法检查器,pylint是静态检查,在用第三方库的时候有些成员只有在运行代码的时候才会被建立,它就找不到成员,在设置(settings.json)里添加 &qu ...

  2. [BZOJ2987]Earthquake:类欧几里得算法

    分析 类欧的式子到底是谁推的啊怎么这么神仙啊orz! 简单说一下这道题,题目中的约束条件可以转化为: \[ y \leq \frac{c-ax}{b} \] 有负数怎么办啊?转化一下: \[ y \l ...

  3. 如何隐藏scroll-Y纵向滚动条,并不影响内容滚动的方法

    网上搜了很多关于隐藏滚动条的文章,发现很多都是只说了如何隐藏scroll-X横向滚动条,对scroll-Y纵向滚动条并没有明确的述说.本文章将介绍3种隐藏滚动条的方法,大家可以结合实际情况,参考文章内 ...

  4. 关于SQL注入的问题以及解决方法

    1.关于SQL注入 什么是SQL注入: 由于jdbc程序在执行的过程中sql语句在拼装时使用了由页面传入参数,如果用户恶意传入一些sql中的特殊关键字,会导致sql语句意义发生变化,这种攻击方式就叫做 ...

  5. shell脚本之浮点数和整数计算

    整数计算 直接使用放括号计算即可,省去*号需要使用转义符的麻烦 #!/bin/bash num1= num2= var1=$[ $num1 * $num2 ] echo "$var1&quo ...

  6. 【重点突破】—— UniApp 微信小程序开发官网学习One

    一.初步认识 uni-app官网:https://uniapp.dcloud.io/component/README HBuilderX官方IDE下载地址: http://www.dcloud.io/ ...

  7. Java 有几种修饰符?分别用来修饰什么

    4种修饰符 访问权限   类   包  子类  其他包 public     ∨   ∨   ∨     ∨ protect    ∨   ∨   ∨     × default    ∨   ∨   ...

  8. Selenium IDE环境部署

    摘自https://blog.csdn.net/ywyxb/article/details/59103683 Selenium IDE环境部署 - Firefox浏览器 Firefox-ESR版本下载 ...

  9. 自动爬取代理IP例子

    import time import json import datetime import threading import requests from lxml import etree from ...

  10. 如何通过shell脚本或一行命令更改root密码?

    哪个能用就用哪个吧! 方法一, echo -e "newpwd\nnewpwd" | (passwd root) 方法二, echo "newpwd" | pa ...