温故知新、勤总结,才能生巧!这次总结一下 :Core Foundation框架到Foundation桥接的三种方式
Foundation提供OC的基础类(像NSObject)、基本数据类型等。
Core Foundation框架 (CoreFoundation.framework) 是一组C语言接口,它们为iOS应用程序提供基本数据管理和服务功能;
XXX为OC的字符串
桥接方式一、
(__bridgeNSString *)(XXX);
桥接方式二、
CFBridgingRelease(XXX)
桥接方式三、
(__bridge_transferNSString*)
(XXX);
(XXX);
Core Foundation的内存管理:
Core Foundation是一组C语言,ARC下,C语言的框架并不会自动释放;
使用桥接方式一的时候,必须需要手动释放内存;
CFRelease(XXX);
在使用桥接方式二、桥接方式三时,则会自动化释放内存;
举例说明:
以一个获取通讯录联系人说明:
首先导入通讯录的框架:
1、#import<AddressBookUI/AddressBookUI.h>
2、遵循代理;
3、实现相关代理方法
#import "ViewController.h"
#import <AddressBookUI/AddressBookUI.h>
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
ABPeoplePickerNavigationController *peopleVc = [[ABPeoplePickerNavigationController alloc] init];
peopleVc.peoplePickerDelegate = self;
[self presentViewController:peopleVc animated:YES completion:nil];
}
#pragma -mark ABPeoplePickerNavigationControllerDelegate
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person{
// 桥接方式一
// CFStringRef firstNameRef = ABRecordCopyValue(person, kABPersonSortByFirstName);
// CFStringRef lastNameRef = ABRecordCopyValue(person, kABPersonSortByFirstName);
//
// NSString *firstName = (__bridge NSString *)(firstNameRef);
// NSString *lastName = (__bridge NSString *)(lastNameRef);
// NSLog(@"%@ %@",firstName,lastName);
//
// CFRelease(firstNameRef);
// CFRelease(lastNameRef);
// 桥接方式二
// CFStringRef firstNameRef = ABRecordCopyValue(person, kABPersonSortByFirstName);
// CFStringRef lastNameRef = ABRecordCopyValue(person, kABPersonSortByFirstName);
//
// NSString *firstName = CFBridgingRelease(firstNameRef);
// NSString *lastName = CFBridgingRelease(lastNameRef);
// NSLog(@"%@ %@",firstName,lastName);
// 桥接方式三
CFStringRef firstNameRef = ABRecordCopyValue(person, kABPersonSortByFirstName);
CFStringRef lastNameRef = ABRecordCopyValue(person, kABPersonSortByFirstName);
NSString *firstName = (__bridge_transfer NSString *)(firstNameRef);
NSString *lastName = (__bridge_transfer NSString *)(lastNameRef);
NSLog(@"%@ %@",firstName,lastName);
}
@end