当前位置:网站首页>A brief understanding of the in arc__ bridge、__ bridge_ Retained and__ bridge_ transfer
A brief understanding of the in arc__ bridge、__ bridge_ Retained and__ bridge_ transfer
2022-07-07 20:59:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
Source of the article :http://www.outflush.com/2015/03/introduction-of-arc-bridge-type-transfer/
In the face of bridge Before the explanation of relevant modifiers . First understand the following
- Core Foundation It's a group. C Language interface . It is associated with Foundation Provide interfaces for the same functions . just Foundation The framework provides Objective-C Interface .
- Core Foundation The concept of reference counting also exists for objects in , is equivalent to Foundation Of retain/release, The corresponding interface is CFRetain/CFRelease
- The objects of these two frameworks can be converted to each other , Such a transformation is called Toll-Free bridge
- When using ARC when ,Core Foundation Objects in are not ARC Managed by . therefore Core Foundation and Foundation In the process of mutual conversion of objects in, it will involve the conversion of all rights of objects . Here we use bridge Modifier .
Ordinary objects and C Conversion between language pointers
void *p = NULL;
{
id obj = [[NSObject alloc] init];
p = (__bridge void *)obj;
}
NSLog(@"Hello");
// There will be errors
NSLog(@"%@", [(__bridge id)p class]);In the above code obj By ARC management ,p It's a C Language pointer . be not in ARC Within the scope of management . When the program runs to obj Beyond the scope of .ARC It will be obj to release fall , At this time p The pointer becomes NULL. So it's using __bridge You must be clear about the life cycle of the object, otherwise similar errors will occur .
Then we should use __bridge_retain Keyword to convert
p = (__bridge_retain void *)obj;
// The above code is in non ARC Can be expressed as
p = obj;
[(id)p retain];So when obj By ARC release after ,p The pointer still points to a valid object .
and __bridge_transfer Is used to pass a __bridge_retain The result of the transformation C Once again, the language pointer is converted to be ARC Common objects managed .
id obj = (__bridge_transfer id)p;
// Use non ARC To express is to
id obj = p;
[obj retain];
[(id)p release];Can see ,__bridge_transfer take p The ownership of the object pointed to is transferred to ARC Managed obj On .
When in ARC Declare in the environment id obj when . The default is strong Decorated with a modifier , therefore ARC Will take the initiative to obj Conduct retain Handle . So __bridge_transfer Just did it release Handle .
Core Foundation And Foundation Conversion between ordinary objects
We already know from above Core Foundation The concept of reference counting also exists for objects in . When in non ARC In the environment ,Core Foundation Objects and Foundation Objects can pass the standard C Language type conversion (Toll-Free bridge). And when introduced ARC Then you need bridge To switch , Because you need to clearly tell the compiler how to handle all the rights of objects .
such as :
NSString *str = [NSString stringWithFormat…];
CFStringRef cfStr = (__bridge CFStringRef)str;
...
CFRelease(cfStr);here str The object is ARC Managed by , and cfStr It's not in ARC In the management of , because __bridge Simply type conversion , So when str By ARC release after ,cfStr It becomes NULL.
When the above code uses __bridge_retain After conversion .cfStr Then I have str Ownership of the object , Now suppose str By ARC release,cfStr Still valid .
However, due to Core Foundation The concept of reference counting also exists for objects in . So we need to use CFRelease() Manual pair cfStr Conduct release operation .
The code is as follows :
NSString *str = [NSString stringWithFormat…];
CFStringRef cfStr = (__bridge_retain CFStringRef)str;
...
CFRelease(cfStr);as for __bridge_transfer, It can be seen from the above that it is used to transfer the full right of the object , therefore CF(Core Foundation Abbreviation ) Object is using __bridge_transfer Convert to Foundation Object is released .
CFStringRef cfStr = CFStringCreate…();
NSString *str = (__bridge_transfer NSString *)cfStr;
// In Africa ARC In the environment , The above sentence is equivalent to
NSString *str = cfStr;
CFRelease(cfStr);actually , stay Core Foundation There are two internal for CF Objects and Foundation Object conversion function
CFTypeRef CFBridgingRetain(id X) {
return (__bridge_retained CFTypeRef)X;
}
id CFBridgingRelease(CFTypeRef X) {
return (__bridge_transfer id)X;
}Using these two functions, you can perform type conversion between the two objects .
summary
- bridge Used to be ARC The object of management and not ARC Conversion between managed objects
- __bridge Only responsible for simple type conversion , Pay special attention to the life cycle of the object .
- __bridge_retain Will be ARC The managed object is converted to not be ARC Manage the object at the same time , take ARC The object of Management retain. Make its part not to be ARC The object of Management ( The description is very inappropriate , Try to figure it out for yourself ).
- __bridge_transfer Will not be ARC The managed object is converted into ARC Manage the object at the same time , Will not be ARC The object of Management release.
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116290.html Link to the original text :https://javaforall.cn
边栏推荐
- 恶魔奶爸 A3阶段 近常速语流初接触
- Cantata9.0 | 全 新 功 能
- How does codesonar help UAVs find software defects?
- Write a jump table
- 使用枚举实现英文转盲文
- 嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
- Is it safe to open an account of BOC shares in kainiu in 2022?
- Intelligent transportation is full of vitality. What will happen in the future? [easy to understand]
- Codeforces Round #296 (Div. 2) A. Playing with Paper[通俗易懂]
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
猜你喜欢
![[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System](/img/76/b725788272ba2dcdf866b28cbcc897.jpg)
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System

C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)

使用枚举实现英文转盲文

【C语言】指针进阶---指针你真的学懂了吗?
Lex & yacc of Pisa proxy SQL parsing
MySQL约束之默认约束default与零填充约束zerofill

Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation

恶魔奶爸 B3 少量泛读,完成两万词汇量+

95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全

Cantata9.0 | new features
随机推荐
Data sorting in string
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
恶魔奶爸 A3阶段 近常速语流初接触
使用高斯Redis实现二级索引
AADL inspector fault tree safety analysis module
Spark 判断DF为空
Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
uva 12230 – Crossing Rivers(概率)「建议收藏」
Cocos2d-x 游戏存档[通俗易懂]
现在网上开户安全么?想知道我现在在南宁,到哪里开户比较好?
Is private equity legal in China? Is it safe?
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
Make this crmeb single merchant wechat mall system popular, so easy to use!
Nebula Importer 数据导入实践
使用高斯Redis实现二级索引
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
OneSpin | 解决IC设计中的硬件木马和安全信任问题
Cantata9.0 | new features
【矩阵乘】【NOI 2012】【cogs963】随机数生成器