Foundation 框架(二)
NSSet(也存在NSMutableSet)
定义:NSSet和数组一样,是个数据容器
区别:
1.数组是有序的,NSSet是无序的
2.数组可以容纳重复的对象,NSSet不能;
3.数组可以重复存入同一个对象,NSSet反之,不能重复存入对象
- NSSet使用
//1.创建NSSet对象
NSSet *set = [[NSSet alloc] initWithObjects:s1,s2,nil];
NSSet *set = [NSSet setWithObjects:s1,s2,nil];
//2.取值
[set anObject];
//3.转数组取值
NSArray *array = [set allObjects];
//4.count 元素的个数
NSUInteger count = [set count];
//5.遍历
for(NSString *s in set){
NSLog(@"s = %@",s);
}
//6.Set转数组
NSSet *set = [NSSet setWithArray:array];
字典
- NSDictionary(key,value)
//1.字典的创建
NSArray *array = @[@"zhangsan",@"lisi"];
NSArray *array = @[@"liming",@"zhangfei"];
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:array,@"ZHANG"
,array,@"ZHANG",nil];
//2.count
NSUInteger count = [dic1 count];
//3.通过key获取到对应的value
[dic objectForKey:@"zhang"];
//4.allKey:获取所有key;
NSArray arrayKey = [dic allKey];
//5.allValue:取得所有value;可以使用点语法
NSArray arrayValue = [dic allValue];
//5.1 通过value获取key
NSArray *array = [dic allKeysForObject:value];
//6.字典的遍历
//(1).普通遍历
NSArray *keys = [dic allKeys];
for(int i= 0;i<keys.count;i++){
NSString *key = keys[i];
NSArray value = [dic objectForKey:key];
NSLog(@"key = %@,value = %@" ,key,value);
}
//(2)快速遍历
for(NSString *key in dic){
NSString *key = keys[i];
NSArray value = [dic objectForKey:key];
NSLog(@"key = %@,value = %@" ,key,value);
}
//7.字典的简便语法
NSDictionary *dic = @{@"zhang":array,@"zhangsan":array1,@"zhangsi":array2};
//(1) 取值
NSArray *array = dic[@"zhang"];
- NSMutableDictionary(key,value)
//1.创建
NSMutableDictionary *mdic = [[NSMutableDictionary alloc] init];
NSMutableDictionary *mdic = [[NSMutableDictionary alloc] initWithCapacity:3];
//2.添加(如果key已存在,会将value 替换)
[mdic setObject:array forKey:@"zhang"];
//3.addEntriesFromDictionary:将另外一个字典的所有元素添加到当前字典中
[mdic addEntriesFromDictionary:(NSDictionary *)]
//4.删除
//(1)
[mdic removeObjectForKey:key];
//(2)
[mdic removeAllObjects];
//(3)根据key删除指定多个元素
[mdic removeObjectsForKeys:@[@"zhang",@"li"]];
- NSNull
定义:空
NSNull *null = [NSNull null];
- 日期类(NSDate)
//1.创建
NSDate *date = [NSDate date];//创建一个时间对象,表示此刻的时间点
NSDate *date = [[NSDate alloc] init];
//2.时间戳
NSTimeInterval timeInterVal = [date timeIntervalSince1970];//此刻的时间戳
//明天此刻的时间点
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval)+24*60*60];
//昨天此刻的时间点
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval)-24*60*60];
//时间戳转NSDate对象
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)];
//3.日期的比较
//(1)通过方法compare:比较
NSComparisonResult result = [date compare:date2];
if(result == NSOrdereAscending){
/*升序,date < date2 */
}else if(result == NSOrdereDescending){
/*降序,date > date2*/
}else if(result == NSOrdereSame){
/*相等,date = date2*/
}
//(2)通过时间戳比较
[date1 timeIntervalSince1970] > [date timeIntervalSince1970]
//-----------------日期的格式化-------------------
NSDate *date = [NSDate date];
//1.日起对象 ----> 字符串对象
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
[dateFormater setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
NSString *dateStr = [dateFormater stringFromDate:date];
//指定时区格式化
//获取所有时区名
NSArray *zoneNames = [NSTimeZone knownTimeZoneNames];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/ShangHai"];
[dateFormater setTimeZone:timeZone];
//2.字符串 ---->日期对象
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]
[dateFormater setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
NSDate *date = [dateFormater dateFromString:@"日期字符串"];
- 异常捕获
/*异常分类
* 1.数组越界
* 2.不能识别的方法选择器
* 3.野指针调用(指针指向的对象已被销毁)
*/
@try{
}@catch(NSException *exception){
}@finally{
}