iOS OC07

阅读: 评论:0

iOS OC07

iOS OC07

迭代器

数组

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->NSNumberNSInteger a=10;NSNumber *number =[[NSNumber alloc] initWithInteger:a];NSLog(@"%@",number);//NSNumber->IntegerNSInteger 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->NSCalueNSValue *value =[NSValue valueWithRange:range];NSLog(@"%@",value);//NSValue->NSRangeNSRange 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/movie/picurl?2583083"},{"movieId":"216391","movieName":"摩登森林之美食总动员","pic_url":"http://v.juhe/movie/picurl?2583246"},{"movieId":"216162","movieName":"龟兔再跑","pic_url":"http://v.juhe/movie/picurl?2583181"},{"movieId":"215977","movieName":"森林孤影","pic_url":"http://v.juhe/movie/picurl?2583247"},{"movieId":"215874","movieName":"从哪来,到哪去","pic_url":"http://v.juhe/movie/picurl?2583542"},{"movieId":"215823","movieName":"有一天","pic_url":"http://v.juhe/movie/picurl?2583092"},{"movieId":"215372","movieName":"兰辉","pic_url":"http://v.juhe/movie/picurl?2583163"}

#

    文件路径NSString *path=@"/Users/dllo/Desktop/上课内容 /OC07_NSDate/OC07_";// 转换成NSDataNSData *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": ".jpg","adapt_url": "/","owner": {"name": "国家博物馆","avatar": ".jpg","uid": "127320","alt": "/","type": "site","id": "127320","large_avatar": ".png"},"loc_name": "北京","alt": "/","id": "21185304","category": "exhibition","title": "罗马与巴洛克艺术","wisher_count": 1975,"has_ticket": false,

#

//    NSString *path=@"/Users/dllo/Desktop/上课内容 /OC07_NSDate/OC07_";
//    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",unt);//返回其中的某一个元素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->NSString1.NSDate *date =[NSDate date];NSString *dateStr=[NSString stringWithFormat:@"%@",date];NSLog(@"%@",dateStr);2.时间的格式yyyy-MM-dd hh-mm-ssy年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);//字符串转换成NSDateNSString *timeStr=@"2015-7-23 17-18-10";NSDate *date=[formatter dateFromString:timeStr];NSLog(@"%@",date);

本文发布于:2024-01-31 20:11:30,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170670309331068.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:iOS
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23