//************* NSSet ********************************
// 不可变集合
// 初始化集合
// 集合中没有重复的元素,而且无序
NSSet *set = [[ NSSet alloc ] initWithObjects : @"1" , @"2" , nil ];
NSLog ( @"set is %@" ,set);
// 类方法初始化
NSSet *set1 = [ NSSet setWithObjects : @"1" , @"2" , @"x" , @"y" , nil ];
NSLog ( @"set1 is %@" ,set1);
// 获取集合中元素的个数
NSInteger count = [set1 count ];
NSLog ( @"count is %ld" ,count);
// 获取集合中所有元素
NSArray *list = [set1 allObjects ];
NSLog ( @"list is %@" ,list);
// 获取集合中任意一个元素
id value = [set1 anyObject ];
NSLog ( @"value is %@" ,value);
// 判断集合中是否包含某各元素
BOOL isTrue = [set1 containsObject : @"x" ];
NSLog ( @"isTrue is %d" ,isTrue);
//*******************************************
// 可变集合
NSMutableSet *mutableSet1 = [ NSMutableSet set ];
NSLog ( @"mutableSet is %@" ,mutableSet1);
// 添加元素
[mutableSet1 addObject : @"x" ];
NSLog ( @"mutableSet is %@" ,mutableSet1);
// 批量添加
NSArray *arr = @[ @"1" , @"2" , @"3" , @"3" , ] ;
[mutableSet1 addObjectsFromArray :arr];
NSLog ( @"mutableSet is %@" ,mutableSet1);
// 删除元素
[mutableSet1 removeObject : @"x" ];
NSLog ( @"mutableSet is %@" ,mutableSet1);
// 删除所有元素
// [mutableSet1 removeAllObjects];
// NSLog(@"mutableSet is %@",mutableSet1);
// 集合的遍历
// 传统遍历
// 需先将集合中的元素转入数组
NSArray *arr1 = [mutableSet1 allObjects ];
for ( int i = 0 ; i < mutableSet1. count ; i++) {
NSString *str = arr1[i];
// 注意:不可以通过下标取集合中的元素
// NSString *str = mutableSet1[1];
NSLog ( @"%@" ,str);
}
NSLog ( @"------------------------" );
// 快速遍历
for ( id value in mutableSet1) {
NSLog ( @"%@" ,value);
// 不可变集合
// 初始化集合
// 集合中没有重复的元素,而且无序
NSSet *set = [[ NSSet alloc ] initWithObjects : @"1" , @"2" , nil ];
NSLog ( @"set is %@" ,set);
// 类方法初始化
NSSet *set1 = [ NSSet setWithObjects : @"1" , @"2" , @"x" , @"y" , nil ];
NSLog ( @"set1 is %@" ,set1);
// 获取集合中元素的个数
NSInteger count = [set1 count ];
NSLog ( @"count is %ld" ,count);
// 获取集合中所有元素
NSArray *list = [set1 allObjects ];
NSLog ( @"list is %@" ,list);
// 获取集合中任意一个元素
id value = [set1 anyObject ];
NSLog ( @"value is %@" ,value);
// 判断集合中是否包含某各元素
BOOL isTrue = [set1 containsObject : @"x" ];
NSLog ( @"isTrue is %d" ,isTrue);
//*******************************************
// 可变集合
NSMutableSet *mutableSet1 = [ NSMutableSet set ];
NSLog ( @"mutableSet is %@" ,mutableSet1);
// 添加元素
[mutableSet1 addObject : @"x" ];
NSLog ( @"mutableSet is %@" ,mutableSet1);
// 批量添加
NSArray *arr = @[ @"1" , @"2" , @"3" , @"3" , ] ;
[mutableSet1 addObjectsFromArray :arr];
NSLog ( @"mutableSet is %@" ,mutableSet1);
// 删除元素
[mutableSet1 removeObject : @"x" ];
NSLog ( @"mutableSet is %@" ,mutableSet1);
// 删除所有元素
// [mutableSet1 removeAllObjects];
// NSLog(@"mutableSet is %@",mutableSet1);
// 集合的遍历
// 传统遍历
// 需先将集合中的元素转入数组
NSArray *arr1 = [mutableSet1 allObjects ];
for ( int i = 0 ; i < mutableSet1. count ; i++) {
NSString *str = arr1[i];
// 注意:不可以通过下标取集合中的元素
// NSString *str = mutableSet1[1];
NSLog ( @"%@" ,str);
}
NSLog ( @"------------------------" );
// 快速遍历
for ( id value in mutableSet1) {
NSLog ( @"%@" ,value);
}
//************ NSNumber *************************
// 封装基本数据类型
int intValue = 10 ;
float floatValue = 3.23 ;
BOOL boolValue = true ;
NSNumber *intNum = [[ NSNumber alloc ] initWithInt :intValue];
NSNumber *floatNum = [[ NSNumber alloc ] initWithFloat :floatValue];
NSNumber *boolNum = [[ NSNumber alloc ] initWithBool :boolValue];
NSLog ( @"intNum is %@, floatNum is %@, boolNum is %@" ,intNum, floatNum, boolNum);
// 包装后可以放到容器中
NSArray *arr = @[ intNum, floatNum, boolNum ] ;
NSLog ( @"arr is %@" ,arr);
// 还原为基本数据类型
int value1 = [intNum intValue ];
float value2 = [floatNum floatValue ];
BOOL value3 = [boolNum boolValue ];
NSLog ( @"value1 is %d, value2 is %.2f, value3 is %d" ,value1, value2, value3);
// 封装基本数据类型
int intValue = 10 ;
float floatValue = 3.23 ;
BOOL boolValue = true ;
NSNumber *intNum = [[ NSNumber alloc ] initWithInt :intValue];
NSNumber *floatNum = [[ NSNumber alloc ] initWithFloat :floatValue];
NSNumber *boolNum = [[ NSNumber alloc ] initWithBool :boolValue];
NSLog ( @"intNum is %@, floatNum is %@, boolNum is %@" ,intNum, floatNum, boolNum);
// 包装后可以放到容器中
NSArray *arr = @[ intNum, floatNum, boolNum ] ;
NSLog ( @"arr is %@" ,arr);
// 还原为基本数据类型
int value1 = [intNum intValue ];
float value2 = [floatNum floatValue ];
BOOL value3 = [boolNum boolValue ];
NSLog ( @"value1 is %d, value2 is %.2f, value3 is %d" ,value1, value2, value3);
//************ NSValue **************************
//NSValue
可以包含任意类型
// 创建一个结构体
NSRange range = { 1 , 4 };
// 将结构体包装成对象
NSValue *value = [ NSValue value :&range withObjCType : @encode ( NSRange )];
NSLog ( @"value is %@" ,value);
// 还原结构体
NSRange newRange;
[value getValue :&newRange];
NSLog ( @"newRange.location is %ld,newRange.length is %ld" ,newRange. location ,newRange. length );
NSLog ( @"newRange is %@" , NSStringFromRange (newRange));
//NSNull
// 表示一个空对象
NSNull *null = [ NSNull alloc ];
NSArray *arr = @[ @"1" , @"2" ,null, @"4" ] ;
// 创建一个结构体
NSRange range = { 1 , 4 };
// 将结构体包装成对象
NSValue *value = [ NSValue value :&range withObjCType : @encode ( NSRange )];
NSLog ( @"value is %@" ,value);
// 还原结构体
NSRange newRange;
[value getValue :&newRange];
NSLog ( @"newRange.location is %ld,newRange.length is %ld" ,newRange. location ,newRange. length );
NSLog ( @"newRange is %@" , NSStringFromRange (newRange));
//NSNull
// 表示一个空对象
NSNull *null = [ NSNull alloc ];
NSArray *arr = @[ @"1" , @"2" ,null, @"4" ] ;
NSLog(@"arr is %@",arr);
//*********** 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 ( @"time2010 is %.2f" ,time2001);
// 到当前的时间
NSTimeInterval time = [date timeIntervalSinceNow ];
NSLog ( @"time is %.2f" ,time);
// 获取昨天的时间
NSTimeInterval oneDay = 24 * 60 * 60 ; // 先计算出一天的秒数
NSDate *yesterday = [[ NSDate alloc ] initWithTimeIntervalSinceNow :-oneDay];
NSLog ( @"yestoday is %@" ,yesterday);
// 获取明天的时间
NSDate *tommorrow = [[ NSDate alloc ] initWithTimeIntervalSinceNow :oneDay];
NSLog ( @"tommorrow is %@" ,tommorrow);
// 获取将来的时间(最大值)
NSDate *future = [ NSDate distantFuture ];
NSLog ( @"future is %@" ,future);
// 获取过去的时间
NSDate *past = [ NSDate distantPast ];
NSLog ( @"past is %@" ,past);
//============ 日期的比较 =============================
// 比较两个时间是否相同 ---- (无意义)
BOOL isEqual = [date isEqualToDate :date1];
NSLog ( @"isEqual is %d" ,isEqual);
// 返回两个时间中较早的那个时间
NSDate *earlierDate = [tommorrow earlierDate :yesterday];
NSLog ( @"earlierDate is %@" ,earlierDate);
// 返回两个时间中较晚的那个时间
NSDate *laterDate = [tommorrow laterDate :yesterday];
NSLog ( @"laterDate is %@" ,laterDate);
//====================================================
//------------- 重要!!! ------------------------------
// 将时间戳转换成日期
NSTimeInterval seconed = 1234567890 ;
NSDate *date2 = [ NSDate dateWithTimeIntervalSince1970 :seconed];
NSLog ( @"date2 is %@" ,date2);
// 格式化日期类
NSDateFormatter *df = [[ NSDateFormatter alloc ] init ];
[df setDateFormat : @"yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒 ZZZZ" ];
// 将日期按照格式化类型转换成字符串
NSString *str = [df stringFromDate :date];
NSLog ( @"str is %@" ,str);
// 将字符串格式化为日期
NSDate *date3 = [df dateFromString :str];
// 获取当前系统时间 标准时间 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 ( @"time2010 is %.2f" ,time2001);
// 到当前的时间
NSTimeInterval time = [date timeIntervalSinceNow ];
NSLog ( @"time is %.2f" ,time);
// 获取昨天的时间
NSTimeInterval oneDay = 24 * 60 * 60 ; // 先计算出一天的秒数
NSDate *yesterday = [[ NSDate alloc ] initWithTimeIntervalSinceNow :-oneDay];
NSLog ( @"yestoday is %@" ,yesterday);
// 获取明天的时间
NSDate *tommorrow = [[ NSDate alloc ] initWithTimeIntervalSinceNow :oneDay];
NSLog ( @"tommorrow is %@" ,tommorrow);
// 获取将来的时间(最大值)
NSDate *future = [ NSDate distantFuture ];
NSLog ( @"future is %@" ,future);
// 获取过去的时间
NSDate *past = [ NSDate distantPast ];
NSLog ( @"past is %@" ,past);
//============ 日期的比较 =============================
// 比较两个时间是否相同 ---- (无意义)
BOOL isEqual = [date isEqualToDate :date1];
NSLog ( @"isEqual is %d" ,isEqual);
// 返回两个时间中较早的那个时间
NSDate *earlierDate = [tommorrow earlierDate :yesterday];
NSLog ( @"earlierDate is %@" ,earlierDate);
// 返回两个时间中较晚的那个时间
NSDate *laterDate = [tommorrow laterDate :yesterday];
NSLog ( @"laterDate is %@" ,laterDate);
//====================================================
//------------- 重要!!! ------------------------------
// 将时间戳转换成日期
NSTimeInterval seconed = 1234567890 ;
NSDate *date2 = [ NSDate dateWithTimeIntervalSince1970 :seconed];
NSLog ( @"date2 is %@" ,date2);
// 格式化日期类
NSDateFormatter *df = [[ NSDateFormatter alloc ] init ];
[df setDateFormat : @"yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒 ZZZZ" ];
// 将日期按照格式化类型转换成字符串
NSString *str = [df stringFromDate :date];
NSLog ( @"str is %@" ,str);
// 将字符串格式化为日期
NSDate *date3 = [df dateFromString :str];
NSLog(@"date3 is %@",date3);
//***********
异常处理
******************************
NSArray *array = @[ @"1" , @"2" ] ;
@try {
// 尝试着去执行以下代码,看编译器是否会报错
NSLog ( @"%@" ,array[ 2 ]);
}
@catch (NSException *exception) {
// 如果捕获到异常,则执行以下代码
NSLog ( @"exception name is %@, reson is %@" ,exception. name , exception. reason );
}
@finally {
// 不管是否捕获到异常,都会执行
NSLog ( @"finally" );
}
NSArray *array = @[ @"1" , @"2" ] ;
@try {
// 尝试着去执行以下代码,看编译器是否会报错
NSLog ( @"%@" ,array[ 2 ]);
}
@catch (NSException *exception) {
// 如果捕获到异常,则执行以下代码
NSLog ( @"exception name is %@, reson is %@" ,exception. name , exception. reason );
}
@finally {
// 不管是否捕获到异常,都会执行
NSLog ( @"finally" );
}