从通用类型继承Swift [英] Swift Inherit from Generic Type

查看:489
本文介绍了从通用类型继承Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class我试图从一个泛型类型继承,这样我就可以将一个类型插入一个层次结构中: Foo< T:AnyObject> :T {} 

但我得到错误

< pre $ 从非协议继承,非类类型'T'



但是我确保T是一个类(即使我将AnyObject更改为某个其他类)。这是不可能的?

解决方案

如果您不知道它是什么,理由。


  • 其中之一就是,编译器需要知道如何在内存中布局类的实例。要做到这一点,它需要知道它具有的实例变量(存储属性):类指定的类和从超类继承的类。

  • 编译器还需要了解超类的属性和初始化方法,以确保类的实例已正确初始化。如果超类的属性是未知的,那么在初始化器结束之前,没有办法知道你是否已经初始化了你应该使用的所有状态(并且不应该)。如果超类的初始值未知,编译器不能强制你从你自己调用超类的指定初始值设定项。
  • 如何编写方法实现那个超类的引用属性和方法,如果你不知道超类是什么?如果你编写引用 self.view 的代码,那么不知怎的,实例化你的类的一个版本不会从具有该属性的类继承,那么代码就会中断。



可能还有其他几个原因,但这些应该足够了。 :)

尽管有一些地方你可能会被子类化,但Cocoa(无论是在Swift还是ObjC中)通常都支持组合而不是定制的继承。


如果你将许多功能添加到各种视图控制器中,并通过将代码分解到它自己的类中来重用代码,那么简单的解决方案是使你的视图控制器类都是拥有该类的一个实例,而不是试图那个类。如果您想要强制执行某些视图控制器类必须拥有 Foo 的地方,则可以定义一个表示该需求的协议。 / p>

如果您尝试向现有课程添加功能,则可以使用扩展名。您可能无法使用扩展程序来添加存储的属性,但您可以伪造它。例如,您可以使用计算属性,并在其实现中定义自己的存储。一种方法可能是使用ObjC运行时的关联对象功能(该AFAIK仍然是可从Swift访问)。

I'm trying to inherit from a generic type, so that I can insert a type into a hierarchy:

class Foo < T:AnyObject > : T {}

but I get error

inheritance from non-protocol, non-class type 'T'

but I'm ensuring T is a class (even if I change AnyObject to some other class). Is this just not possible?

解决方案

You can't inherit from something if you don't know what it is, for multiple reasons.

  • For one, the compiler needs to know how to lay out an instance of your class in memory. To do that, it needs to know what instance variables (stored properties) it has: both those the class specifies and those inherited from a superclass.

  • The compiler also needs to know about the superclass' properties and initializers to make sure that an instance of your class is properly initialized. If the superclass' properties are unknown, there's no way to know if you've initialized all the state that you should (and none that you shouldn't) before your initializer ends. If the superclass' initializers are unknown, the compiler can't enforce that you call a superclass' designated initializer from your own.

  • How will you write method implementations that reference properties and methods of the superclass if you don't know what the superclass is? If you write code that references self.view, then somehow instantiate a version of your class that doesn't inherit from a class with that property, that code would break.

There are probably several more reasons, but these should be enough. :)

Though there are a few places where you're expected to subclass, Cocoa (whether in Swift or ObjC) generally favors composition instead of inheritance for customization.

If you're adding a bunch of functionality to various view controllers, and reusing the code for such by factoring it out into its own class, the simple solution is to make your view controller classes all own an instance of that class instead of trying to be that class. If you have places where you want to enforce that certain view controller classes must own a Foo, you can define a protocol that expresses that requirement.

If you're trying to add functionality to an existing class, you can use an extension. You might not be able to use an extension to add stored properties, but you can certainly fake it. For example, you could use computed properties, and define your own storage within their implementation. One way to do that might be to use the associated objects feature of the ObjC runtime (which AFAIK is still accessible from Swift).

这篇关于从通用类型继承Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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