ARC结构中的Objective-C类 [英] Objective-C classes in structs with ARC

查看:125
本文介绍了ARC结构中的Objective-C类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试制作一个带有类的结构,如:

I tried making a struct with classes in it like:

struct my_struct
{
    NSString *string;
    // more fields
};

令我惊讶的是,Objective-C ++在启用ARC的情况下允许了此操作.
如何管理字符串?
它可以轻松保留在每个作业中,但问题是释放.
它可以添加一个带有release的析构函数,但这将使该结构变得不平凡.
它还可以使它不保留或释放,但是这样做应该存在unsafe_unretained.

To my surprise, Objective-C++ allowed this with ARC enabled.
How will it manage the string?
It can easily retain in each assignment but release is the problem.
It can add a destructor with release in it, but this will make the struct non-trivial.
It can also make this not retain or release, but to do so there should be unsafe_unretained.

根据我的观察,使用此工具时不会崩溃,但是我想知道这里真正发生了什么.

From my observation nothing crashes when using this, but I would like to know what really happens here.

推荐答案

请参见第4.3.5页ARC文档:

4.3.5.所有权合格的结构和联合字段

4.3.5. Ownership-qualified fields of structs and unions

如果程序声明C结构或联合的成员,则格式不正确 具有非平凡的所有权限定类型.

A program is ill-formed if it declares a member of a C struct or union to have a nontrivially ownership-qualified type.

合理比例:从C ++的角度来看,结果类型将是非POD,但是C 并没有为我们提供很好的语言工具来管理 聚集,因此简单地禁止使用它们会更方便.它是 仍然可以使用void *或__unsafe_unretained来管理此问题 对象.

Rationale: the resulting type would be non-POD in the C++ sense, but C does not give us very good language tools for managing the lifetime of aggregates, so it is more convenient to simply forbid them. It is still possible to manage this with a void* or an __unsafe_unretained object.

此限制不适用于Objective-C ++.但是,非常重要 拥有资格的类型被认为是非POD的:在C ++ 11术语中,它们 并非默认默认可构造,不可复制构造,移动 可构造的,可复制的可分配的,可移动的可分配的或可破坏的.它 违反了C ++的一个定义规则",无法在外部使用类 ARC,在ARC之下,拥有不平凡的所有权资格 成员.

This restriction does not apply in Objective-C++. However, nontrivally ownership-qualified types are considered non-POD: in C++11 terms, they are not trivially default constructible, copy constructible, move constructible, copy assignable, move assignable, or destructible. It is a violation of C++'s One Definition Rule to use a class outside of ARC that, under ARC, would have a nontrivially ownership-qualified member.

合理性:与C语言不同,我们可以表达所有必要的ARC语义 用于所有权限定的子对象作为(默认)的子操作 类的特殊成员函数.这些功能然后变成 不平凡的.这具有班级将具有的非显而易见的结果 非平凡的副本构造函数和非平凡的析构函数;如果这 在ARC之外通常不是true,该类型的对象将是 通过并返回了与ABI不兼容的方式.

Rationale: unlike in C, we can express all the necessary ARC semantics for ownership-qualified subobjects as suboperations of the (default) special member functions for the class. These functions then become non-trivial. This has the non-obvious result that the class will have a non-trivial copy constructor and non-trivial destructor; if this would not normally be true outside of ARC, objects of the type will be passed and returned in an ABI-incompatible manner.

如果您阅读了所有注意事项,强烈建议您不要在ObjC ++中这样做.我强烈建议不要在任何情况下都广泛使用ObjC ++.这是一种桥接语言,可帮助纯ObjC和纯C ++相互通信.它有很多问题.将ObjC ++与ARC结合使用会带来时间和空间性能成本,这在ObjC中是不会发生的,从而使其具有异常安全性.定义这些特定于ObjC ++的数据结构使它很难与非ObjC ++代码和非ARC代码进行交互(请注意,您不能在ARC之外使用它).您必须再次担心内存管理(正如您已经发现的那样),您应该从ARC免费获得的许多东西突然变得很难.

If you read through all the caveats, I would strongly recommend against doing this in ObjC++. I strongly recommend against extensive use of ObjC++ in any case. It is a bridging language to help pure ObjC and pure C++ talk to each other. It has many problems. Combining ObjC++ with ARC introduces both time and space performance costs that do not occur in ObjC in order to make it exception-safe. Defining these kinds of ObjC++-specific data structures makes it hard to interact with non-ObjC++ code and non-ARC code (note the caveat that you cannot use this outside of ARC). Much of what you should get for free from ARC suddenly becomes hard as you have to worry again about memory management (as you've already discovered).

构建一个纯ObjC层.构建一个纯C ++层.构建一个薄的ObjC ++层以将两者捆绑在一起.不要将ObjC对象放在结构中,绝对不要放在任何公共结构中(即在定义它的单个ObjC ++对象外部可见).

Build a pure-ObjC layer. Build a pure C++ layer. Build a thin ObjC++ layer to tie the two together. Do not put ObjC objects in structs, and definitely not in any public structs (i.e. visible outside of the single ObjC++ object that defines it).

这篇关于ARC结构中的Objective-C类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆