Foundation与CoreFoundation联系

无缝桥接:(ARC)
id oc_Variable=(__bridge_transfer id)c_Variable;//把C类型变量所有权转换到OC类型变量上(c类型转oc类型)

CFTypeRef c_Variable=(__bridge_retained CFTypeRef)oc_Variable;//让C类型变量持有OC类型变量(oc类型转c类型)
CFRelease(c_Variable);//c类型变量不在ARC管理内,使用完要手动释放其内存

CFTypeRef c_Variable=(__bridge CFTypeRef)oc_Variable;//(oc类型转c类型),但C类型变量不持有OC类型变量所有权,oc_Variable所有权仍归它所有
//CFRelease(c_Variable);//因C类型变量不持有OC类型变量所有权,所以无需释放该变量所指内存


Foundation常用不可变collection及各类型快速遍历方式:
NSDictionary *dic=@{@"key1":@"value1",@"key2":@"value2"};
for(NSString *key in dic){
NSLog(@"value with key:%@ in dic is :%@",key,dic[key]);
}/*
value with key:key1 in dic is :value1
value with key:key2 in dic is :value2
*/
[dic enumerateKeysAndObjectsUsingBlock:^(/*id*/NSString *key, /*id*/NSString *obj, BOOL *stop) {
NSLog(@"value with key:%@ in dic is :%@",key,dic[key]);
if ([key isEqualToString:@"key1"]) {
NSLog(@"had found key1 in dic and key1 value is %@,then stop enumerate.",obj);
*stop=YES;
}
}];/*
value with key:key1 in dic is :value1
had found key1 in dic and key1 value is value1,then stop enumerate.
*/

NSArray *arr=@[@"value1",@"value2"];
for(NSString *value in arr){
NSLog(@"value in arr is %@",value);
}/*
value in arr is value1
value in arr is value2
*/
for(NSString *value in [arr reverseObjectEnumerator]){
NSLog(@"value in reverseObjectEnumerator with arr is %@",value);
}/*
value in reverseObjectEnumerator with arr is value2
value in reverseObjectEnumerator with arr is value1
*/
[arr enumerateObjectsUsingBlock:^(/*id*/NSString *obj, NSUInteger idx, BOOL *stop) {
NSLog(@"value in arr of index:%lu is %@",(unsigned long)idx,obj);
if ([obj isEqualToString:@"value1"]) {
NSLog(@"had found value is value1 in arr,then stop enumerate.");
*stop=YES;
}
}];/*
value in arr of index:0 is value1
had found value is value1 in arr,then stop enumerate.
*/

NSSet *set=[NSSet setWithObjects:@"value1",@"value2",@"value3", nil];
for(NSString *value in set){
NSLog(@"value in set is %@",value);
}/*
value in set is value2
value in set is value1
value in set is value3
*/
[set enumerateObjectsUsingBlock:^(/*id*/NSString *obj, BOOL *stop) {
NSLog(@"value in set is %@",obj);
if ([obj isEqualToString:@"value1"]) {
NSLog(@"had found value is value1 in set,then stop enumerate.");
*stop=YES;
}
}];/*
value in set is value2
value in set is value1
had found value is value1 in set,then stop enumerate.
*/

Foundation中用于缓存的collection:
NSCache *cache=[[NSCache alloc]init];
NSPurgeableData *save_PurgeableData1=[[NSPurgeableData alloc]init];
NSPurgeableData *save_PurgeableData2=[[NSPurgeableData alloc]init];
NSPurgeableData *save_PurgeableData3=[[NSPurgeableData alloc]init];
[cache setObject:save_PurgeableData1 forKey:@"cache1"];
[cache setObject:save_PurgeableData2 forKey:@"cache2"];
[cache setObject:save_PurgeableData3 forKey:@"cache3"];

NSPurgeableData *use_PurgeableData4=[cache objectForKey:@"cache4"];
if (use_PurgeableData4) {
[use_PurgeableData4 beginContentAccess];
/*use use_PurgeableData4*/
[use_PurgeableData4 endContentAccess];
}else{
NSPurgeableData *save_PurgeableData4=[NSPurgeableData dataWithData:[[NSData alloc]init]];
/*创建时use_PurgeableData4保留数会多1,无需调用[use_PurgeableData4 beginContentAccess];*/
[cache setObject:save_PurgeableData4 forKey:@"cache4" cost:save_PurgeableData4.length];
/*use use_PurgeableData4*/
[use_PurgeableData4 endContentAccess];
}
NSCache对比NSDictionary作为缓存集合的好处:
1.NSCache当内存紧张时提供优雅的自动删减功能,而且是线程安全的,且不会拷贝键。
2.NSCache对象可以设置上限,用于限制缓存中的对象总个数及“总成本”,而这个“总成本”控制着缓存删减其中对象的时机
3.NSPurgeableData和NSCache搭配使用,可实现自动清楚数据功能,也就是当NSPurgeableData对象所占内存被系统丢弃时,对象自身也会从缓存中移除
4.只有那种“重新计算起来很费事的”数据,才值得放入缓存,比如那些需要从网络获取或从磁盘读取的数据。

posted @ 2016-03-13 13:42  Jk_Chan  阅读(1229)  评论(0编辑  收藏  举报