不能子类化 DispatchGroup - “只能通过 Objective-C 运行时可见"? [英] Can't subclass DispatchGroup - "only visible via the Objective-C runtime"?

查看:30
本文介绍了不能子类化 DispatchGroup - “只能通过 Objective-C 运行时可见"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不能子类化DispatchGroup,怎么做?

注意:

例如,携带一个带状态的包,

Example, carry a stateful package with a group,

class PushDispatchGroup: DispatchGroup {
    var sentIds: [(tableName: String, rowId: String)] = []
}

感谢shallowThought 指出iOS10 中已修复此问题.

thanks to shallowThought for pointing out this is fixed in iOS10.

推荐答案

如何从仅通过 Objective-C 运行时可见"的类继承?

how to inherit from a class which is 'only visible via the Objective-C runtime'?

你没有.

这并没有像您考虑的那样围绕一个适当的对象".它包裹在一个 dispatch_group 周围,它是一个 C 结构体.这个类不是为了子类化而设计的,也不会以正常方式桥接到 ObjC,所以你也不能在那里子类化它.

This is not wrapped around a proper "object" the way you're thinking about it. It's wrapped around a dispatch_group, which is a C struct. This class is not designed to be subclassed and does not bridge in a normal way to ObjC, so you can't subclass it there either.

直接桥接到低级 C 类型的对象"通常具有非常不寻常的结构,系统的某些部分被硬编码以知道如何处理(这种情况一直发生在像 NSString 这样的免费桥接中CFString).在许多情况下,通过仔细选择结构,这些类型被设计为在内存布局上是相同的(我没有选择 DispatchGroup,但它看起来像这个组中的一个).如果是这样,您将无法添加任何存储空间,因为这样内存布局会有所不同,并且会破坏桥接.

"Objects" that bridge directly to low level C types often have very unusual structures that parts of the system are hard-coded to know how to deal with (this happens all the time with toll-free bridging like NSString and CFString). In many cases, the types are designed to be identical in memory layout through careful choice of structure (I haven't picked apart DispatchGroup, but it looks like one from this group). When that's true, you can't add any storage because then the memory layout will be different and you break the bridging.

正如各种评论者所说,您不应该这样做,这就是为什么没有简单的答案如何做到这一点.没有明确为子类化设计的类在 Swift 中很难或不可能子类化(这经常出现在比 DispatchGroup 更复杂的类型周围,答案是一样的:它是故意的;不要这样做它).

As various commenters have said, you also shouldn't be doing this, which is why there is no easy answer for how to do this. Classes that are not explicitly designed for subclassing are intentionally difficult or impossible to subclass in Swift (this comes up regularly around less tricky types than DispatchGroup, and the answer is the same: it's intentional; don't do it).

这篇关于不能子类化 DispatchGroup - “只能通过 Objective-C 运行时可见"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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