OC学习 第八节 NSData NSSet

===================
day8 Foundation杂类
===================

# 回顾
C模拟内存的引用计数
—————————————————————
# 模拟自定义autoreleasepool
—————————————————————
# 基本数据类型怎么存放到OC数组中?
NSNumber initWithLong:… initWithInt…
longValue intValue
numberWithDouble:…
doubleVaule
比较两个数字对象的内容:[num1 isEqualToNumber:num2]
@123
—————————————————————

# NSValue
包装 int long …
// int --> NSValue
int age;
NSValue *intValue = [NSValue valueWithBytes:&age objCType:@encode(int)];

// NSValue --> int
int newAge;
[intValue getValue:&newAge];
NSLog(@"%d", newAge);

// double --> NSValue
double score = 3.14;
NSValue *scoreValue = [NSValue valueWithBytes:&score objCType:@encode(double)];

// NSValue --> double
double newScore;
[scoreValue getValue:&newScore];
NSLog(@"%lf", newScore);

// 判断数据类型
NSLog(@"%s", scoreValue.objCType);
NSLog(@"%s", @encode(double));
// 判断数据类型
if (strcmp(scoreValue.objCType, @encode(double)) == 0) {
    NSLog(@"double类型");
}

// 官方已经封装好了一些函数 专门转化 NSRange NSSize NSRect
NSRange ran = {100,3};
NSValue *ranValue = [NSValue valueWithRange:ran];
NSRange newRan = [ranValue rangeValue];

# 如何把一个字符串的字串范围NSRange保存在OC数组? NSRange -> struct
NSValue :可以把任意的数据类型转化为 NSValue对象
那么C语言的数组类型 和结构体类型 等这些复合的数据类型 都可以通过NSValue 进行转换
示例:
typedef  struct _Range {
    NSInteger location;
    NSInteger length;
}MyRange;

MyRange range1 = {1,3};
如何把这样一个结构体放进数组呢?
转成NSValue对象:
// MyRange —> NSValue
/*
         - (id)initWithBytes:(const void *)value objCType:(const char *)type;
         功能:创建NSValue对象
         第一个参数:传要转化的数据类型空间的地址
         第二个参数:类型字符串  需要用@encode(类型名)
         */
NSValue *value = [[NSValue alloc] initWithBytes:&range1 objCType:@encode(MyRange)];
// [NSValue valueWithBytes:(const void *) objCType:(const char *)];

// NSValue --> MyRange
MyRange newRange;
[value getValue:&newRange];
NSLog(@"loc: %ld len: %ld", newRange.location, newRange.length);

—————————————————————

NSNull 如何往数组中插入一个空对象

—————————————————————

NSSet/NSMutableSet
// 集合类的元素 可以是任意类型的对象地址
NSSet *set1 = [[NSSet alloc] initWithObjects:@"one",@1,@"oc",@"ios",@"ui", nil];
NSLog(@"set1:%@",set1);

// 通过类方法 实例化一个NSSet对象
NSSet *set2 = [NSSet setWithObjects:@"oc",@"ios",@"ui", nil];
NSLog(@"set2:%@",set2);

NSLog(@"count:%ld",set1.count); //集合元素的个数
[set1 release];

// 在存放集合元素的时候是没有相同的元素,如果出现相同的那么只会存一个
NSSet *set3 = [NSSet setWithObjects:@"ui",@"oc",@"ios",@"ios", nil];

NSLog(@"set3:%@",set3);
if ([set3 isEqualToSet:set2]) {
    NSLog(@"集合内容相同");
}

// 判断是否是子集合
NSSet *set4 = [NSSet setWithObjects:@"oc",@"ui", nil];

if ([set4 isSubsetOfSet:set3]) {
    NSLog(@"set4指向的集合是set3指向的集合的子集合");
}

// 根据一个数组创建一个新的集合
NSArray *arr = @[@"one",@"two",@"three",@"four"];
NSSet *set5 = [NSSet setWithArray:arr];
NSLog(@"set5:%@",set5);

// 把一个集合转化为一个数组
NSArray *newArr = [set4 allObjects];
NSLog(@"newArr:%@",newArr);

// 可变集合
NSMutableSet * mutSet = [NSMutableSet setWithObjects:@"one", nil];

// 增加元素
[mutSet addObject:@"oc"];
NSLog(@"mut:%@",mutSet);

// 删除指定的元素
[mutSet removeObject:@"one"];
NSLog(@"mut:%@",mutSet);

[mutSet addObject:@"ui"];
[mutSet addObject:@"ios"];

NSSet *set6 = [NSSet setWithObjects:@"java",@"c#", nil];
NSSet *set7 = [NSSet setWithObjects:@"oc", nil];
NSLog(@"mut:%@",mutSet);
// 把一个集合增加到另外一个集合中
// [mutSet unionSet:set6];
// 删除一个集合中得元素
[mutSet minusSet:set7];
NSLog(@"mut:%@",mutSet);

// 遍历集合
for (NSString *str in mutSet) {
    NSLog(@"str:%@",str);
}

NSArray *arr3 = @[@"one",@"two",@"three",@"four",@"five",@"six"];

//数字集合 /索引集合
NSIndexSet *indexs = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 3)];
//把 一个范围内的数字转化为一个数字集合对象 集合元素是1 ,2 ,3
//把数组arr3的 索引值 1 2 3的元素提取出来构成一个新的子数组
NSArray *subArr = [arr3 objectsAtIndexes:indexs];
NSLog(@"sub:%@",subArr);

//提取 数组arr3中得 索引值分别是0 2 4对应得元素
//1.创建1个空得可变索引集合
NSMutableIndexSet *mutIndexs = [NSMutableIndexSet indexSet];
//增加数字索引
[mutIndexs addIndex:0];
[mutIndexs addIndex:2];
[mutIndexs addIndex:4];

NSArray *subArr2  = [arr3 objectsAtIndexes:mutIndexs];
NSLog(@"sub2:%@",subArr2);

—————————————————————

NSDate  时间类
//获取当前系统时间
//NSDate *date = [[NSDate alloc] init];
NSDate *date = [NSDate date];
//GMT 格林威治时间
NSLog(@"date:%@",date);

//创建一个距离当前系统时间 10s之后的一个时间
NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:10];
NSLog(@"date2:%@",date2);

//1970 01-01 00:00:00 计算机元年
//距离计算机元年 30s之后 的一个时间
毫秒 1000*60*60*24*365*100
NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:30];
NSLog(@"date3:%@",date3);

NSDate *nowDate = [NSDate date];
NSLog(@"now:%@",nowDate);
//计算时间差 NSTimeInterval  秒-》单位
NSTimeInterval subT = [nowDate timeIntervalSinceDate:date];
NSLog(@"sub:%f",subT);//执行创建date 和nowDate 代码的时间差

//计算nowDate 距离 计算机元年的一个时间差
NSTimeInterval subT2 = [nowDate timeIntervalSince1970];
NSLog(@"sub2:%f",subT2);

//距离nowDate  1小时之后的一个时间
NSDate *newDate = [nowDate dateByAddingTimeInterval:60*60];
NSLog(@"newDate:%@",newDate);
//nowDate 一天之后的时间
NSDate *newDate2 = [nowDate dateByAddingTimeInterval:24*60*60];
NSLog(@"newDate2:%@",newDate2);

//获取两个时间中较早的一个时间
NSDate *d2 = [nowDate earlierDate:newDate];
NSLog(@"d2:%@",d2);
//获取两个时间中较晚的一个时间
NSDate *d3 =  [nowDate laterDate:newDate];
//比较两个时间是否相等
BOOL ret = [nowDate isEqualToDate:newDate];

//获取遥远的过去的一个时间
NSDate *past = [NSDate distantPast];
NSLog(@"past:%@",past);
//获取遥远的将来的一个时间
NSDate *future = [NSDate distantFuture];
NSLog(@"future:%@",future);

—————————————————————

NSTimeZone
NSDate *date = [NSDate date];
NSLog(@"%@", date);

//获取当前系统的时区
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
//算出当前系统时区和格林威治时区的时间差
NSTimeInterval sub = [timeZone secondsFromGMTForDate:date];

//推算出本地真正时间
NSDate *localTimer = [date dateByAddingTimeInterval:sub];
NSLog(@"loc:%@",localTimer);

—————————————————————
NSDateFormatter
NSDate *date = [NSDate date];
//时间格式化 对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设置时间格式化
/*
 yyyy 设置年 4位
 MM   月
 dd   天
 
 hh 12进制小时
 mm  分钟
 ss  秒
 
 HH 24进制
 
 EEEE 星期几
 */
dateFormatter.dateFormat = @"yyyy年MM月dd日 HH:mm:ss EEEE";

//把NSDate 的时间对象 转化为时间字符串(会转化为真正的本地时间)
//把date时间对象按照dateFormatter的时间格式 进行转换
NSString *dateString = [dateFormatter stringFromDate:date];
//dateString已经按照本地时区转换了
NSLog(@"dateStr:%@",dateString);

[dateFormatter release];


NSDateFormatter *f2 = [[NSDateFormatter alloc] init];

f2.dateFormat = @"yyyy年MM月dd日 HH:mm:ss";

NSString *t = @"2014年11月28日 13:01:01";//必须要和时间格式一致
//把一个时间字符串按照 时间格式进行转化 转换为NSDate 类型
NSDate *newDate = [f2 dateFromString:t];
NSLog(@"newdate:%@",newDate);
[f2 release];
—————————————————————
—————————————————————

官方的类可以进行拷贝
//NSString  NSMutableString  NSArray NSMutableArray
NSDictionary NSMutableDictionary  NSSet NSMutableSet
NSData NSMutableData
对于上述系统的类 copy 是意思:把一个对象拷贝成不可变对象
上述系统的类mutableCopy  的意思:把一个对象拷贝成可变对象


# 单例
+ (MyPlane *)defaultPlane {
    static MyPlane * plane = nil;
    //如果有多个线程进行操作那么这些线程会排队一个一个进行
    @synchronized(self) {//同步  考虑线程安全
        if (plane ==  nil) {
            plane = [[self alloc] init];
        }
    }
    
    return plane;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值