1.MRC環(huán)境下橋接
- (void)MRC{
//MRC下橋接
//Foundation到CoreFoundation框架的數(shù)據(jù)類型轉(zhuǎn)換
NSString*str = [[NSStringalloc]init];
//CFStringRef ,這種轉(zhuǎn)換,屬于直接轉(zhuǎn)換,不會(huì)移交對(duì)象的內(nèi)存管理
CFStringRefstrRef = (CFStringRef)str;
NSLog(@"%@",strRef);
[strrelease];
//CoreFoundation到Foundation框架的數(shù)據(jù)類型轉(zhuǎn)換
CFStringRefstrRef2 =CFStringCreateWithCString(CFAllocatorGetDefault(),"123",kCFStringEncodingUTF8);
//這種轉(zhuǎn)換文兢,屬于直接轉(zhuǎn)換抒线,不會(huì)移交對(duì)象的內(nèi)存管理
NSString*str2 = (NSString*)strRef2;
NSLog(@"%@",str2);
CFRelease(strRef2);
}
2.ARC環(huán)境下橋接
- (void)ARC{
//ARC下橋接
//Foundation到CoreFoundation框架的數(shù)據(jù)類型轉(zhuǎn)換
NSString*str = [[NSStringalloc]init];
//(__bridge CFStringRef)等同于MRC下面的直接轉(zhuǎn)換熔掺,不會(huì)移交對(duì)象的內(nèi)存管理
//CFStringRef strRef = (__bridge CFStringRef)(str);
//CFBridgingRetain == __bridge_retained CFStringRef
//這種轉(zhuǎn)換,會(huì)移交對(duì)象的內(nèi)存管理
CFStringRefstrRef = (__bridge_retainedCFStringRef)(str);
NSLog(@"%@",strRef);
CFRelease(strRef);
//CoreFoundation到Foundation框架的數(shù)據(jù)類型轉(zhuǎn)換
CFStringRefstrRef2 =CFStringCreateWithCString(CFAllocatorGetDefault(),"123",kCFStringEncodingUTF8);
//__bridge NSString *等同于MRC下面的直接轉(zhuǎn)換钙勃,不會(huì)移交對(duì)象的內(nèi)存管理
//NSString *str2 = (__bridge NSString *)strRef2;
//CFBridgingRelease == __bridge_transfer NSString *
//這種轉(zhuǎn)換,會(huì)移交對(duì)象的內(nèi)存管理
NSString*str2 = (__bridge_transferNSString*)strRef2;
NSLog(@"%@",str2);
// CFRelease(strRef2);
}