您的位置:首页 > 移动开发 > IOS开发

iOS OC07_NSDate

2015-07-23 19:16 405 查看

迭代器

数组

NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"2",@"3",@"4", nil];
NSEnumerator *enumerator =[arr objectEnumerator];
NSString *str =nil;
while (str=[enumerator nextObject]) {
NSLog(@"%@",str);
}


打印结果

2015-07-23 17:49:36.208 OC07_NSDate[1722:148494] 1
2015-07-23 17:49:36.209 OC07_NSDate[1722:148494] 2
2015-07-23 17:49:36.209 OC07_NSDate[1722:148494] 3
2015-07-23 17:49:36.209 OC07_NSDate[1722:148494] 4


打印字典的value

NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"2",@"3",@"4", nil];
NSEnumerator *enumer =[dic objectEnumerator];
NSString *str =nil;
while (str=[enumer nextObject]) {
NSLog(@"%@",str);
}


打印结果

2015-07-23 17:58:49.490 OC07_NSDate[1738:151076] 1
2015-07-23 17:58:49.491 OC07_NSDate[1738:151076] 3


值对象

把基本数据类型转换成对象类型

//Integer->NSNumber
NSInteger a=10;
NSNumber *number =[[NSNumber alloc] initWithInteger:a];
NSLog(@"%@",number);
//NSNumber->Integer
NSInteger b=[number integerValue];
NSNumber *num=@10;


打印结果

2015-07-23 19:01:59.813 OC07_NSDate[1787:161605] 10


NSValue

把结构体和指针换成对象类型

结构体的.是访问成员变量,对象调用属性是点语法

NSRange range={2,5};
NSLog(@"%ld",range.length);
// NSRange->NSCalue
NSValue *value =[NSValue valueWithRange:range];
NSLog(@"%@",value);
//NSValue->NSRange
NSRange rangeV=[value rangeValue];


打印结果

2015-07-23 19:06:35.140 OC07_NSDate[1794:163546] 5
2015-07-23 19:06:35.140 OC07_NSDate[1794:163546] NSRange: {2, 5}


int a=10;
int*p=&a;
NSValue *pointer=[NSValue valueWithPointer:p];

int *p1=[pointer pointerValue];


两个寻找路径

文档

{"reason":"success","result":[{"movieId":"216609","movieName":"诡八楼","pic_url":"http:\/\/v.juhe.cn\/movie\/picurl?2583083"},{"movieId":"216391","movieName":"摩登森林之美食总动员","pic_url":"http:\/\/v.juhe.cn\/movie\/picurl?2583246"},{"movieId":"216162","movieName":"龟兔再跑","pic_url":"http:\/\/v.juhe.cn\/movie\/picurl?2583181"},{"movieId":"215977","movieName":"森林孤影","pic_url":"http:\/\/v.juhe.cn\/movie\/picurl?2583247"},{"movieId":"215874","movieName":"从哪来,到哪去","pic_url":"http:\/\/v.juhe.cn\/movie\/picurl?2583542"},{"movieId":"215823","movieName":"有一天","pic_url":"http:\/\/v.juhe.cn\/movie\/picurl?2583092"},{"movieId":"215372","movieName":"兰辉","pic_url":"http:\/\/v.juhe.cn\/movie\/picurl?2583163"}


#

文件路径
NSString *path=@"/Users/dllo/Desktop/上课内容 /OC07_NSDate/OC07_NSDate/movielist.txt";

// 转换成NSData
NSData *data=[NSData dataWithContentsOfFile:path];

NSMutableDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",dic);
NSMutableArray *arr=dic[@"result"];
NSLog(@"%@",arr);

NSDictionary *movieDic=arr[0];
NSLog(@"%@",movieDic);

NSString *str=movieDic[@"movieName"];
NSLog(@"%@",str);

NSString *movieName=dic[@"result"][1][@"movieName"];
NSLog(@"%@",movieName);

NSString *pic_url=dic[@"result"][1][@"pic_url"];
NSLog(@"%@",pic_url);

//打印电影名
NSArray *arr=dic[@"result"];
for (NSDictionary *dic in arr) {
NSLog(@"%@",dic[@"movieName"]);
}


文档

{
"count": 20,
"start": 0,
"total": 4344,
"events": [
{
"image": "http://img3.douban.com/view/event_poster/median/public/10f53a2ad8b38c5.jpg",
"adapt_url": "http://www.douban.com/location/partner/event/21185304/",
"owner": {
"name": "国家博物馆",
"avatar": "http://img3.douban.com/view/site/small/public/6510ea4ea1cc8e6.jpg",
"uid": "127320",
"alt": "http://site.douban.com/127320/",
"type": "site",
"id": "127320",
"large_avatar": "http://img3.douban.com/pics/site/icon_default_large.png"
},
"loc_name": "北京",
"alt": "http://www.douban.com/event/21185304/",
"id": "21185304",
"category": "exhibition",
"title": "罗马与巴洛克艺术",
"wisher_count": 1975,
"has_ticket": false,


#

//    NSString *path=@"/Users/dllo/Desktop/上课内容 /OC07_NSDate/OC07_NSDate/activitylist.txt";
//    NSData *data=[NSData dataWithContentsOfFile:path];
//    NSMutableDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//////    NSString *zhaolei=dic[@"events"][2][@"ownner"][@"name"];
//////    NSLog(@"%@",zhaolei);
////    //拼接
////    NSString *begin_time=dic[@"events"][2][@"begin_time"];
////    NSString *end_time=dic[@"events"][2][@"end_time"];
////    //截取
////    NSString *newEnd=[end_time substringFromIndex:10];
////    NSString *newTime=[NSString stringWithFormat:@"%@-%@",begin_time,newEnd];
////    NSLog(@"%@",newTime);
//    NSInteger i=0;
//    for (NSDictionary *temp in dic[@"events"]) {
//        if ([temp[@"category"] isEqualToString: @"film"]) {
//            i++;
//
//        }
//    }NSLog(@"%ld",i);


NSSet集合

NSSet是无序的

NSSet不能有重复的元素

集合里的元素必须是对象类型

NSSet *set=[NSSet setWithObjects:@"3",@"2",@"6",@"4",@"5",@"2", nil];
NSLog(@"%@",set);
NSLog(@"%ld",set.count);
//返回其中的某一个元素
NSLog(@"%@",[set anyObject]);
//判断是否有
NSLog(@"%d",[set containsObject:@"3"]);


结果

2015-07-23 19:13:37.278 OC07_NSDate[1819:166400] {(
3,
6,
4,
2,
5
)}
2015-07-23 19:13:37.279 OC07_NSDate[1819:166400] 5
2015-07-23 19:13:37.279 OC07_NSDate[1819:166400] 3
2015-07-23 19:13:37.279 OC07_NSDate[1819:166400] 1


普通方式创建一个集合对象
NSSet *set=[[NSSet alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil ];
NSLog(@"%@",set);

可变集合
NSMutableSet *set=[NSMutableSet setWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
NSLog(@"%@",set);

//添加元素
[set addObject:@"6"];
NSLog(@"%@",set);
//删除
[set removeObject:@"3"];
NSLog(@"%@",set);

//forin进行遍历
for (NSString *temp in set) {
NSLog(@"%@",temp);
}


NSDate

NSDate是一个日期的类
NSDate *date =[NS;
+date获取的时间无论在哪个区,都是打印相对应的零时区的时间

获取一下当前所在的时区
NSTimeZone *zone=[NSTimeZone systemTimeZone];
NSLog(@"%@",zone);

// 获取一下和0时区相差的秒数
NSInteger seconds =[zone secondsFromGMTForDate:date];
NSLog(@"%ld",seconds);

// 通过相差的秒数,能获取到现在的时间
NSDate *locatDate=[NSDate dateWithTimeIntervalSinceNow:seconds];
NSLog(@"%@",locatDate);

//    NSDate *time=[NSDate dateWithTimeIntervalSince1970:0];
//    NSLog(@"%@",time);
//
//    // 一个是明天这个时候

NSDate *tomorrowTime=[NSDate dateWithTimeIntervalSinceNow:(32*3600)];
NSLog(@"%@",tomorrowTime);

// 昨天这个时候的时间

NSDate *yeaTime=[NSDate dateWithTimeIntervalSinceNow:(-16*3600)];
NSLog(@"%@",yeaTime);

// 时间间隔
// 对应double类型
// 计算两个对象的时间间隔
//    NSTimeInterval interval=[tomorrowTime timeIntervalSinceDate:date];
//    NSLog(@"%g",interval);

NSDate *date1=[NSDate date];
NSDate *date2=[NSDate dateWithTimeIntervalSinceNow:1000];
NSTimeInterval interval =[date2 timeIntervalSinceDate:date1];
if (interval <= 60) {
NSLog(@"刚刚");
}else if (interval > 60 && interval <= 3600){
NSLog(@"%ld分钟以前",((NSInteger)interval / 60));
}else if (interval > 3600 && interval < (3600*24)){
NSLog(@"%ld小时以前",((NSInteger)interval / 3600));
}

把日期和字符串进行一个相互转换
NSDate->NSString
1.
NSDate *date =[NSDate date];
NSString *dateStr=[NSString stringWithFormat:@"%@",date];
NSLog(@"%@",dateStr);
2.时间的格式
yyyy-MM-dd hh-mm-ss
y年
M月
d日
h小时 H24小时 h12小时
m分钟
s秒

先设置时间的格式,要转换的时间和格式相吻合

NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
//    NSDate *date=[NSDate date];
//    //通过格式,把指定的时间转换成NSString
//    //通过这种方式,系统还会把时间切换成当前的时间
//    NSString *strDate=[formatter stringFromDate:date];
//    NSLog(@"%@",strDate);

//字符串转换成NSDate
NSString *timeStr=@"2015-7-23 17-18-10";
NSDate *date=[formatter dateFromString:timeStr];
NSLog(@"%@",date);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: