【iOS】集合间相互转换,浅谈相关内存管理,使用NSData处理数据,使用NSDate

版权声明:本文为博主原创,如需转载请注明出处。

NSArray 转换成 NSMutableArray

1
2
3
NSArray * array = @[@"one",@"two",@"three"];
NSMutableArray * muArray = [NSMutableArray arrayWithArray:array];
NSLog(@"muarray %@",muArray);

NSDictonary 转换成 NSMutableDictionary

1
2
3
NSDictionary * dic = @{@"one":@"1",@"two":@"2"};
NSMutableDictionary * muDic = [NSMutableDictionary dictionaryWithDictionary:dic];
NSLog(@"mudic %@ ",muDic);

NSset 转换成 NSMutableSet

1
2
3
NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two", nil];
NSMutableSet *muSet = [NSMutableSet setWithSet:set];
NSLog(@"muSet %@",muSet);

NSArray 转换成NSSet

1
2
NSMutableSet * muSet2 = [NSMutableSet setWithArray:array];
NSLog(@"muSet2 %@",muSet2);

NSDictionary 转化成 NSArray

1
2
3
4
NSArray * allkeys = [dic allKeys];
NSLog(@"allkeys %@",allkeys);
NSArray * allValues = [dic allValues];
NSLog(@"allValues %@",allValues);

NSString 转换成 NSArray

1
2
3
NSString * str = @"www.yoferzhang.com";
NSArray * strArray = [str componentsSeparatedByString:@"."];
NSLog(@"strArray %@",strArray);

谈一谈字典数组集合的手动内存管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int main(int argc, const char * argv[])
{

@autoreleasepool {

// retainCount 1
Person * p = [[Person alloc] init];

//1
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:0];

//当你把对象存入到,数组中的时候,数组会对这个对象进行一次 retain操作
[array addObject:p];// [p retain] retaiCount 2

//当你把一个对象移除数组中的时候,会对该对象进行一次 release操作 retainCount 1
// [array removeObject:p];
//会对数组中所有的对象,进行一次,relsease操作
// [array removeAllObjects];

//当集合对象被销毁的时候,会对其内部所有的对象进行一次 release操作
//0
[array release];
//retainCount - 0


//0
[p release];

NSLog(@"ddddd");

}
return 0;
}

谈一谈字典数组集合的ARC机制内存管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//ARC机制,是否需要担心内存溢出呢
//谁告诉你不用的心得啊:道理就是下面的示

int main(int argc, const char * argv[])
{

@autoreleasepool {

Person * p = [[Person alloc] init];

NSMutableArray * array = [[NSMutableArray alloc] init];

//当一个对象被存入集合中的时候,默认会保存它的强指针
[array addObject:p];

//当个把一个对象从集合中,删除的时候,会释放掉这个对象的强指针
// [array removeObject:p];

// [array removeAllObjects];

array = nil;

p = nil;

NSLog(@"adfasdf");

}
return 0;
}

使用 NSData 处理数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int main(int argc, const char * argv[])
{

@autoreleasepool {

//音频文件,文本文件,视频文件就必须使用NSData数据类
/*
NSString * str = @"www.yoferzhang.com";
NSError * error;
[str writeToFile:@"/Users/yoferzhang/Desktop/text.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
*/

/*
NSString * path = @"/Users/yoferzhagn/Desktop/text.txt";
//把目标文件加载到内存中.
//NSData 是不可变长度的Data类型,只能够通过一次加载完成所有的数据
NSData * data = [NSData dataWithContentsOfFile:path];

NSLog(@"data length %ld",data.length);
NSString * pathTo = @"/Users/yoferzhang/Desktop/nextext.txt";
//把加载到内存中的文件,写入到指定的位置
BOOL isWriteSuccess = [data writeToFile:pathTo atomically:YES];

if (isWriteSuccess)
{
NSLog(@"写入成功");
}
else
{
NSLog(@"写入失败");
}
*/


//当需要把多个NSData数据拼接成一个数据存储的时候,就要想到使用NSMutableData这个类型
NSString * str1 = @"好好学习";
NSString * str2 = @"天天向上";
NSString * str3 = @"今天休息";

NSMutableData * muData = [[NSMutableData alloc] init];

NSData * d1 = [str1 dataUsingEncoding:NSUTF8StringEncoding];
NSData * d2 = [str2 dataUsingEncoding:NSUTF8StringEncoding];
NSData * d3 = [str3 dataUsingEncoding:NSUTF8StringEncoding];

//appendData 能够把nsdata对象加入到 muData对象中
[muData appendData:d1];
[muData appendData:d2];
[muData appendData:d3];

NSString * path = @"/Users/yoferzhang/Desktop/测试文件.txt";
//NSMutableData是继承至NSData的所以可以调用writeToFile 把数据写入到一个指定的目录下
BOOL isWriteSuccess = [muData writeToFile:path atomically:YES];
if (isWriteSuccess)
{
NSLog(@"创建成功");
}
else
{
NSLog(@"创建失败");
}

}
return 0;
}

NSDate 日期操作

获得日期对象

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

比较日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
NSTimeInterval secondes = 24 * 60 * 60;

NSDate * now = [NSDate date];

//模拟昨天 24 * 60 * 60
NSDate * yesDay = [now addTimeInterval:-secondes];

//比较两个日期是否相同

BOOL isEqual = [now isEqualToDate:yesDay];
if (isEqual)
{
NSLog(@"相同");
}
else
{
NSLog(@"不相同");
}

//获得两个日期中比较早的一个
NSDate * earler = [yesDay earlierDate:now];
NSLog(@"%@",earler);

NSDate * later = [yesDay laterDate:now];
NSLog(@"later %@",later);

格式化日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*格式化日期操作
NSDate * date = [NSDate date];

//表示格式化日期对象
NSDateFormatter * f = [[NSDateFormatter alloc] init];

[f setDateFormat:@"yyyy-MM-dd hh:mm:ss"];//2014/03/21

//把格式与我们的日期关联起来
NSString * str = [f stringFromDate:date];
NSLog(@"str %@",str);
*/


//把字符串转换成日期
/*
NSString * str = @"2014/03/20";

NSDateFormatter * f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy/MM/dd"];
NSDate * date = [f dateFromString:str];
NSLog(@"date %@",date);
*/


//处理时区问题

NSDate * newDate = [NSDate date];

NSDateFormatter * f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString * str = [f stringFromDate:newDate];
//设置时差计算方法 //GTM
[f setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

NSLog(@"%@",str);

新博客文章地址:集合间相互转换,浅谈相关内存管理,使用NSData处理数据,使用NSDate
CSDN文章地址:集合间相互转换,浅谈相关内存管理,使用NSData处理数据,使用NSDate