OC连载四-----Foundation框架——字典、日期、异常
2015-08-03 08:09
375 查看
/*******************NSDictionary************************/
//初始化一个字典
NSDictionary *dic = [[NSDictionary
alloc]
initWithObjectsAndKeys:@"value",
@"key",
@"value1",
@"key1",
nil];
NSLog(@"dic is %@", dic);
//创建空的字典
NSDictionary *nulldic = [NSDictionary
dictionary];
NSLog(@"nulldic is %@", nulldic);
//创建Person对象
Person *zhangsan = [[Person
alloc]
init];
zhangsan.name
=
@"张三";
zhangsan.age
=
100;
Person *lisi = [[Person
alloc]
init];
lisi.name
=
@"李斯";
lisi.age
=
200;
Person *wangwu = [[Person
alloc]
init];
wangwu.name
=
@"王武";
wangwu.age
=
300;
//创建一个数组
NSArray *names =
@[zhangsan, lisi, wangwu];
NSDictionary *dic2 = [NSDictionary
dictionaryWithObjectsAndKeys:@"v1",
@"k1",
@"v2",
@"k2",
@"v3",
@"k3", names,
@"k4",
nil];
NSLog(@"dic2 is %@", dic2);
//从字典中取值,若key值不存在,则返回null
NSString *str1 = [dic2
objectForKey:@"k1"];
NSString *str2 = [dic2
objectForKey:@"k2"];
NSString *str3 = [dic2
objectForKey:@"k5"];
NSLog(@"str1 is %@", str1);
NSLog(@"str2 is %@", str2);
NSLog(@"str3 is %@", str3);
//取出字典中的数组的第一个元素的姓名
// NSArray *arr = [dic2 objectForKey:@"k4"];
// Person *ps = [arr objectAtIndex:0];
//嵌套调用
Person *ps = [[dic2
objectForKey:@"k4"]
objectAtIndex:0];
NSLog(@"ps name is %@", ps.name);
//字典中键值对个数
NSInteger count = dic2.count;
NSLog(@"count is %ld", count);
//取出字典中所有的key
NSArray *keys = [dic2
allKeys];
NSLog(@"keys is %@", keys);
//取出字典中所有的value
NSArray *values = [dic
allValues];
NSLog(@"values is %@", values);
//字典的快速创建和取值
NSDictionary *dic3 =
@{@"k1":@"v1",@"k2":@"v2",@"k1":@"v1",@"k1":@"v1",};
NSString *v1 = dic3[@"k1"];
NSLog(@"dic3 is %@", dic3);
NSLog(@"v1 is %@", v1);
/******************NSMutableDictionary***************************/
//初始化空的字典
NSMutableDictionary *dic1 = [NSMutableDictionary
dictionary];
//给字典预期的空间
NSMutableDictionary *dic2 = [NSMutableDictionary
dictionaryWithCapacity:10];
//添加元素
[dic1
setObject:@"v1"
forKey:@"k1"];
[dic1
setObject:@"v2"
forKey:@"k2"];
[dic1
setObject:@"v3"
forKey:@"k3"];
[dic1
setObject:@"v4"
forKey:@"k4"];
NSLog(@"dic1 is %@", dic1);
//设置键值对,若key已存在,则是修改key对应的value,如果不存在,则创建一个新的键值对
[dic1
setObject:@"v5"
forKey:@"k3"];
NSLog(@"dic1 is %@", dic1);
//删除元素
[dic1
removeObjectForKey:@"k3"];
NSLog(@"dic1 is %@", dic1);
//删除所有元素
[dic1
removeAllObjects];
NSLog(@"dic1 is %@", dic1);
//字典的遍历
NSDictionary *dic =
@{@"k1":
@"v1",
@"k2":
@"v2",
@"k3":
@"v3",
@"k4":
@"v4",
@"k5":
@"v5",
@"k6":
@"v6"};
NSArray *keys = [dic
allKeys];
//遍历字典
//普通遍历
for (int
i =0; i < dic.count;
i++) {
//获取key
NSString *key = [keys
objectAtIndex:i];
NSString *value = [dic
objectForKey:key];
NSLog(@"key:%@------value:%@", key, value);
}
//快速遍历
for (id
key
in dic) {
//循环块
id value = [dic
objectForKey:key];
NSLog(@"key:%@------value:%@", key, value);
}
/*******************NSSet******************/
//初始化集合
//集合中没有重复元素,而且无序
NSSet *set = [[NSSet
alloc]
initWithObjects:@"a",
@"b",
@"c",
nil];
NSLog(@"set is %@", set);
//类方法初始化
NSSet *set1 = [NSSet
setWithObjects:@"1",
@"2",
@"3",
nil];
NSLog(@"set1 is %@", set1);
//获取集合中元素个数
NSInteger count = [set1
count];
NSLog(@"count is %ld", count);
//获取集合中所有元素
NSArray *arr = set1.allObjects;
NSLog(@"arr is %@", arr);
//获取集合中任意一个元素
id value = [set1
anyObject];
NSLog(@"value is %@", value);
//判断集合中是否含有你、某个元素
BOOL isTure = [set1
containsObject:@"a"];
NSLog(@"isTure is %d", isTure);
/*******************NSMutableSet******************/
//可变集合
NSMutableSet *mutableSet = [NSMutableSet
set];
NSLog(@"mutableSet is %@", mutableSet);
//添加元素
[mutableSet
addObject:@"s"];
NSLog(@"mutableSet is %@", mutableSet);
//批量添加,通过数组
NSArray *arr1 =
@[@"a",
@"x",
@"v",
@"n"];
[mutableSet
addObjectsFromArray:arr1];
NSLog(@"mutableSet is %@", mutableSet);
//删除元素
[mutableSet
removeObject:@"x"];
NSLog(@"mutableSet is %@", mutableSet);
//删除所有元素
// [mutableSet removeAllObjects];
// NSLog(@"mutableSet is %@", mutableSet);
//集合的遍历
//普通遍历,需要将集合转化为数组
NSArray *arr2 = [mutableSet
allObjects];
for (int
i =
0; i < mutableSet.count; i++) {
NSString *str = arr2[i];
//不能直接通过下标取集合的元素,因为集合无序,没有下标
NSLog(@"str is %@", str);
}
NSLog(@"*********************************************************");
//快速遍历
for (id
value
in mutableSet) {
NSLog(@"value is %@", value);
}
/***************NSValue*********************/
//NSValue可以包装任意类型
//创建一个结构体
NSRange range = {1,
5};
//将结构体包装成对象
NSValue *value = [NSValue
value:&range
withObjCType:@encode(NSRange)];
NSLog(@"value is %@", value);
//还原
NSRange newRange;
[value
getValue:&newRange];
NSLog(@"newRange is %@",
NSStringFromRange(newRange));
//NSNull
//表示一个空对象
NSNull *null = [NSNull
null];
NSArray *arr =
@[@"1",
@"3",
@"2",null ,@"3"];
NSLog(@"arr is %@", arr);
/****************NSNumber**************************/
//封装基本数据类型
int intValue =
10;
float floatvalue =
3.14;
BOOL boolValue =
true;
NSNumber *intNumber = [[NSNumber
alloc]
initWithInt:intValue];
NSLog(@"intNumber is %@", intNumber);
NSNumber * floatNumber = [[NSNumber
alloc]
initWithFloat:floatvalue];
NSLog(@"floatNumber is %@", floatNumber);
NSNumber *boolNumber = [[NSNumber
alloc]
initWithBool:boolValue];
NSLog(@"boolNumber is %@", boolNumber);
//包装后放入容器中
NSArray *arr =
@[intNumber, floatNumber, boolNumber
];
NSLog(@"arr is %@", arr);
//还原为基本数据类型
float value1 = [floatNumber
floatValue];
NSLog(@"value1 is %.2f", value1);
int value2 = [intNumber
intValue];
NSLog(@"value2 is %d", value2);
int value3 = [boolNumber
boolValue];
NSLog(@"value3 is %d", value3);
//封包
NSNumber *intNum =
@100;
NSNumber *floatNum =
@9.4;
NSNumber *charNum = @'a';
NSNumber *valuenum =
@(12
+
45);
NSLog(@"valueNum is %@", valuenum);
/******************NSDate********************/
//获取当前系统时间
标准时间
GMT
格林尼治时间
NSDate *date = [NSDate
date];
NSLog(@"date is %@", date);//打印出来是系统当前时间
NSDate *date1 =[[NSDate
alloc]
init];
NSLog(@"date1 is %@", date1);//打印出来是系统当前时间
//获取时间戳
NSTimeInterval time1970 = [date
timeIntervalSince1970];
NSLog(@"time1970 is %.2f", time1970);
//到当前世纪(2001)的时间
NSTimeInterval time2001 =[date
timeIntervalSinceReferenceDate];
NSLog(@"time2001 is %.2f", time2001);
//到当前的时间
NSTimeInterval time = [date
timeIntervalSinceNow];
NSLog(@"time is %.2f", time);
//获取昨天的时间
NSTimeInterval second =
24
* 60
*
60;
NSDate *yesterdayDate = [[NSDate
alloc]
initWithTimeIntervalSinceNow: -second];
NSLog(@"yestodayDate is %@", yesterdayDate);
//获取明天的时间
NSDate *tomorrow = [NSDate
dateWithTimeInterval:second
sinceDate:[NSDate
date]];
NSLog(@"tomorrow is %@", tomorrow);
//获取将来的时间
(最大值)
NSDate *future = [NSDate
distantFuture];
NSLog(@"future is %@", future);
//获取古代时间(以前时间)
NSDate *before = [NSDate
distantPast];
NSLog(@"before is %@", before);
//日期比较//没什么实际作用
// BOOL isTure = [date isEqualToDate:date1];
// NSLog(@"isEqual is %d", isTure);
//下面这些比较重要
//返回两个时间比较早的那个时间
NSDate *earlierDate = [tomorrow
earlierDate:yesterdayDate];
NSLog(@"earlierDate is %@", earlierDate);
//返回两个时间比较晚的那个时间
NSDate *latterDate = [tomorrow
laterDate:yesterdayDate];
NSLog(@"laterDate is %@", latterDate);
//格式化日期类
NSDateFormatter *dateFormatter =[[NSDateFormatter
alloc]
init];
[dateFormatter
setDateFormat:@"yyyy年MM月dd日
HH小时mm分钟ss秒
ZZZZ"];
//将日期按照格式化转化为字符串
NSString *str = [dateFormatter
stringFromDate:date];
NSLog(@"str is %@", str);
//将字符串格式化为日期
NSDate *date3 = [dateFormatter
dateFromString:str];
NSLog(@"date3 iis %@", date3);
/************* try catch finally ************/
NSArray *arr =
@[@"a",
@"s",
@"d"];
@try {
//尝试执行以下代码,看编译器是否会报错
NSLog(@"%@", arr[3]);
}
@catch (NSException *exception) {
//若捕获到异常,则执行以下代码
NSLog(@"exception name is %@ ,reson is %@", exception.name,
exception.reason);
}
@finally {
//无论是否捕获到异常,都执行
NSLog(@"finally");
}
//初始化一个字典
NSDictionary *dic = [[NSDictionary
alloc]
initWithObjectsAndKeys:@"value",
@"key",
@"value1",
@"key1",
nil];
NSLog(@"dic is %@", dic);
//创建空的字典
NSDictionary *nulldic = [NSDictionary
dictionary];
NSLog(@"nulldic is %@", nulldic);
//创建Person对象
Person *zhangsan = [[Person
alloc]
init];
zhangsan.name
=
@"张三";
zhangsan.age
=
100;
Person *lisi = [[Person
alloc]
init];
lisi.name
=
@"李斯";
lisi.age
=
200;
Person *wangwu = [[Person
alloc]
init];
wangwu.name
=
@"王武";
wangwu.age
=
300;
//创建一个数组
NSArray *names =
@[zhangsan, lisi, wangwu];
NSDictionary *dic2 = [NSDictionary
dictionaryWithObjectsAndKeys:@"v1",
@"k1",
@"v2",
@"k2",
@"v3",
@"k3", names,
@"k4",
nil];
NSLog(@"dic2 is %@", dic2);
//从字典中取值,若key值不存在,则返回null
NSString *str1 = [dic2
objectForKey:@"k1"];
NSString *str2 = [dic2
objectForKey:@"k2"];
NSString *str3 = [dic2
objectForKey:@"k5"];
NSLog(@"str1 is %@", str1);
NSLog(@"str2 is %@", str2);
NSLog(@"str3 is %@", str3);
//取出字典中的数组的第一个元素的姓名
// NSArray *arr = [dic2 objectForKey:@"k4"];
// Person *ps = [arr objectAtIndex:0];
//嵌套调用
Person *ps = [[dic2
objectForKey:@"k4"]
objectAtIndex:0];
NSLog(@"ps name is %@", ps.name);
//字典中键值对个数
NSInteger count = dic2.count;
NSLog(@"count is %ld", count);
//取出字典中所有的key
NSArray *keys = [dic2
allKeys];
NSLog(@"keys is %@", keys);
//取出字典中所有的value
NSArray *values = [dic
allValues];
NSLog(@"values is %@", values);
//字典的快速创建和取值
NSDictionary *dic3 =
@{@"k1":@"v1",@"k2":@"v2",@"k1":@"v1",@"k1":@"v1",};
NSString *v1 = dic3[@"k1"];
NSLog(@"dic3 is %@", dic3);
NSLog(@"v1 is %@", v1);
/******************NSMutableDictionary***************************/
//初始化空的字典
NSMutableDictionary *dic1 = [NSMutableDictionary
dictionary];
//给字典预期的空间
NSMutableDictionary *dic2 = [NSMutableDictionary
dictionaryWithCapacity:10];
//添加元素
[dic1
setObject:@"v1"
forKey:@"k1"];
[dic1
setObject:@"v2"
forKey:@"k2"];
[dic1
setObject:@"v3"
forKey:@"k3"];
[dic1
setObject:@"v4"
forKey:@"k4"];
NSLog(@"dic1 is %@", dic1);
//设置键值对,若key已存在,则是修改key对应的value,如果不存在,则创建一个新的键值对
[dic1
setObject:@"v5"
forKey:@"k3"];
NSLog(@"dic1 is %@", dic1);
//删除元素
[dic1
removeObjectForKey:@"k3"];
NSLog(@"dic1 is %@", dic1);
//删除所有元素
[dic1
removeAllObjects];
NSLog(@"dic1 is %@", dic1);
//字典的遍历
NSDictionary *dic =
@{@"k1":
@"v1",
@"k2":
@"v2",
@"k3":
@"v3",
@"k4":
@"v4",
@"k5":
@"v5",
@"k6":
@"v6"};
NSArray *keys = [dic
allKeys];
//遍历字典
//普通遍历
for (int
i =0; i < dic.count;
i++) {
//获取key
NSString *key = [keys
objectAtIndex:i];
NSString *value = [dic
objectForKey:key];
NSLog(@"key:%@------value:%@", key, value);
}
//快速遍历
for (id
key
in dic) {
//循环块
id value = [dic
objectForKey:key];
NSLog(@"key:%@------value:%@", key, value);
}
/*******************NSSet******************/
//初始化集合
//集合中没有重复元素,而且无序
NSSet *set = [[NSSet
alloc]
initWithObjects:@"a",
@"b",
@"c",
nil];
NSLog(@"set is %@", set);
//类方法初始化
NSSet *set1 = [NSSet
setWithObjects:@"1",
@"2",
@"3",
nil];
NSLog(@"set1 is %@", set1);
//获取集合中元素个数
NSInteger count = [set1
count];
NSLog(@"count is %ld", count);
//获取集合中所有元素
NSArray *arr = set1.allObjects;
NSLog(@"arr is %@", arr);
//获取集合中任意一个元素
id value = [set1
anyObject];
NSLog(@"value is %@", value);
//判断集合中是否含有你、某个元素
BOOL isTure = [set1
containsObject:@"a"];
NSLog(@"isTure is %d", isTure);
/*******************NSMutableSet******************/
//可变集合
NSMutableSet *mutableSet = [NSMutableSet
set];
NSLog(@"mutableSet is %@", mutableSet);
//添加元素
[mutableSet
addObject:@"s"];
NSLog(@"mutableSet is %@", mutableSet);
//批量添加,通过数组
NSArray *arr1 =
@[@"a",
@"x",
@"v",
@"n"];
[mutableSet
addObjectsFromArray:arr1];
NSLog(@"mutableSet is %@", mutableSet);
//删除元素
[mutableSet
removeObject:@"x"];
NSLog(@"mutableSet is %@", mutableSet);
//删除所有元素
// [mutableSet removeAllObjects];
// NSLog(@"mutableSet is %@", mutableSet);
//集合的遍历
//普通遍历,需要将集合转化为数组
NSArray *arr2 = [mutableSet
allObjects];
for (int
i =
0; i < mutableSet.count; i++) {
NSString *str = arr2[i];
//不能直接通过下标取集合的元素,因为集合无序,没有下标
NSLog(@"str is %@", str);
}
NSLog(@"*********************************************************");
//快速遍历
for (id
value
in mutableSet) {
NSLog(@"value is %@", value);
}
/***************NSValue*********************/
//NSValue可以包装任意类型
//创建一个结构体
NSRange range = {1,
5};
//将结构体包装成对象
NSValue *value = [NSValue
value:&range
withObjCType:@encode(NSRange)];
NSLog(@"value is %@", value);
//还原
NSRange newRange;
[value
getValue:&newRange];
NSLog(@"newRange is %@",
NSStringFromRange(newRange));
//NSNull
//表示一个空对象
NSNull *null = [NSNull
null];
NSArray *arr =
@[@"1",
@"3",
@"2",null ,@"3"];
NSLog(@"arr is %@", arr);
/****************NSNumber**************************/
//封装基本数据类型
int intValue =
10;
float floatvalue =
3.14;
BOOL boolValue =
true;
NSNumber *intNumber = [[NSNumber
alloc]
initWithInt:intValue];
NSLog(@"intNumber is %@", intNumber);
NSNumber * floatNumber = [[NSNumber
alloc]
initWithFloat:floatvalue];
NSLog(@"floatNumber is %@", floatNumber);
NSNumber *boolNumber = [[NSNumber
alloc]
initWithBool:boolValue];
NSLog(@"boolNumber is %@", boolNumber);
//包装后放入容器中
NSArray *arr =
@[intNumber, floatNumber, boolNumber
];
NSLog(@"arr is %@", arr);
//还原为基本数据类型
float value1 = [floatNumber
floatValue];
NSLog(@"value1 is %.2f", value1);
int value2 = [intNumber
intValue];
NSLog(@"value2 is %d", value2);
int value3 = [boolNumber
boolValue];
NSLog(@"value3 is %d", value3);
//封包
NSNumber *intNum =
@100;
NSNumber *floatNum =
@9.4;
NSNumber *charNum = @'a';
NSNumber *valuenum =
@(12
+
45);
NSLog(@"valueNum is %@", valuenum);
/******************NSDate********************/
//获取当前系统时间
标准时间
GMT
格林尼治时间
NSDate *date = [NSDate
date];
NSLog(@"date is %@", date);//打印出来是系统当前时间
NSDate *date1 =[[NSDate
alloc]
init];
NSLog(@"date1 is %@", date1);//打印出来是系统当前时间
//获取时间戳
NSTimeInterval time1970 = [date
timeIntervalSince1970];
NSLog(@"time1970 is %.2f", time1970);
//到当前世纪(2001)的时间
NSTimeInterval time2001 =[date
timeIntervalSinceReferenceDate];
NSLog(@"time2001 is %.2f", time2001);
//到当前的时间
NSTimeInterval time = [date
timeIntervalSinceNow];
NSLog(@"time is %.2f", time);
//获取昨天的时间
NSTimeInterval second =
24
* 60
*
60;
NSDate *yesterdayDate = [[NSDate
alloc]
initWithTimeIntervalSinceNow: -second];
NSLog(@"yestodayDate is %@", yesterdayDate);
//获取明天的时间
NSDate *tomorrow = [NSDate
dateWithTimeInterval:second
sinceDate:[NSDate
date]];
NSLog(@"tomorrow is %@", tomorrow);
//获取将来的时间
(最大值)
NSDate *future = [NSDate
distantFuture];
NSLog(@"future is %@", future);
//获取古代时间(以前时间)
NSDate *before = [NSDate
distantPast];
NSLog(@"before is %@", before);
//日期比较//没什么实际作用
// BOOL isTure = [date isEqualToDate:date1];
// NSLog(@"isEqual is %d", isTure);
//下面这些比较重要
//返回两个时间比较早的那个时间
NSDate *earlierDate = [tomorrow
earlierDate:yesterdayDate];
NSLog(@"earlierDate is %@", earlierDate);
//返回两个时间比较晚的那个时间
NSDate *latterDate = [tomorrow
laterDate:yesterdayDate];
NSLog(@"laterDate is %@", latterDate);
//格式化日期类
NSDateFormatter *dateFormatter =[[NSDateFormatter
alloc]
init];
[dateFormatter
setDateFormat:@"yyyy年MM月dd日
HH小时mm分钟ss秒
ZZZZ"];
//将日期按照格式化转化为字符串
NSString *str = [dateFormatter
stringFromDate:date];
NSLog(@"str is %@", str);
//将字符串格式化为日期
NSDate *date3 = [dateFormatter
dateFromString:str];
NSLog(@"date3 iis %@", date3);
/************* try catch finally ************/
NSArray *arr =
@[@"a",
@"s",
@"d"];
@try {
//尝试执行以下代码,看编译器是否会报错
NSLog(@"%@", arr[3]);
}
@catch (NSException *exception) {
//若捕获到异常,则执行以下代码
NSLog(@"exception name is %@ ,reson is %@", exception.name,
exception.reason);
}
@finally {
//无论是否捕获到异常,都执行
NSLog(@"finally");
}
相关文章推荐
- ios--c DAY_3
- myeclipse无法新建jsp页面
- C:进制
- (笔记)鼠标移动到一个 元素上所触发的事件
- 黑马程序员——自学java基础课程第二…
- 【bzoj1079】【SCOI2008】【着色方案】
- hibernate一对多等情况关联查询效率低的解决方法
- 素数筛法
- 【Unity游戏开发之五】游戏目录结构之最佳实践和优化
- 素数筛法
- Linux 桌面领域的八大最新潮流
- Linux 桌面领域的八大最新潮流
- POJ 2251 Dungeon Master
- HDOJ~~A+B Problem
- DFS BestCoder Round #49 ($) 1001 Untitled
- BC - Untitled
- 精通iOS
- 黑马程序员——Map集合(映射)
- UIImageView图片填充三种模式的区别
- andriod viewholder的使用