Swift:覆盖子类中的typealias [英] Swift: overriding typealias inside subclass

查看:138
本文介绍了Swift:覆盖子类中的typealias的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在想我的项目中的一个自定义模式,但我无法让它工作。主要想法是更改每个子类上的 typealias 以访问子类特定的接口。

  protocol InstanceInterface:class {

typealias接口

var interface:接口{get}
}

// Baseclass
协议FirstClassInterface:class {/ *这里没有示例* /}
$ b $ class FirstClass:InstanceInterface,FirstClassInterface {

typealias Interface = FirstClassInterface

var interface:接口{返回self}
}

//子类
协议SecondClassInterface:FirstClassInterface {

func foo ()
}

class SecondClass:FirstClass,SecondClassInterface {

typealias Interface = SecondClassInterface //< ---这样做什么都没有:(

func foo(){print(hello world)} // Swift 2.0 here
}

//比方说我想通过界面$ b $调用foo b让测试=第二Class()

test.interface.foo()//'Interface'没有名为'foo'的成员

有什么我做错了吗?我误解了一些Swift概念吗?!我确实需要在这里继承,不要一次又一次地从超类的协议中实现一切。我的小模式甚至有可能吗?我会很感激任何帮助。 :)

解决方案

不幸的是,这个问题没有很好的解决方法。



重写 typealias 的主要思想可以在这种情况下工作,但需要考虑以下几点:

  protocol TakeAndGet {
typealias T
func take(value:T)
func get() - > T
}

class FirstClass:TakeAndGet {
typealias T = FirstClass

var property = 0

func take值:T){
value.property = 4
}

func get() - > T {
返回FirstClass()
}
}

class SecondClass:FirstClass {

typealias T = SecondClass

var property2 =hello
}

如果 > SecondClass 的> typealias 会覆盖另一个 take 方法,一个可以被视为超类的子类。但是 get 方法不能隐式地将 FirstClass 转换为 SecondClass 。因此,无法重载 typealias



现在,如果我们要覆盖 get() - >获得函数。 SecondClass 它不起作用,因为它与超类中的签名不同。另外,我们继承了 get 方法,导致模糊用法:

  SecondClass()。get()//返回哪个类型? SecondClass或FirstClass 

所以你必须尝试一种不同的方法。


So I was thinking about a custom pattern in my project, but I can't get it to work. The main idea is to change the typealias on every subclass to get access to the subclass specific interface.

protocol InstanceInterface: class {

    typealias Interface

    var interface: Interface { get }
}

// Baseclass
protocol FirstClassInterface: class { /* nothing here for the example */ }

class FirstClass: InstanceInterface, FirstClassInterface {

    typealias Interface = FirstClassInterface

    var interface: Interface { return self }
}

// Subclass
protocol SecondClassInterface: FirstClassInterface { 

    func foo()
}

class SecondClass: FirstClass, SecondClassInterface {

    typealias Interface = SecondClassInterface // <--- This does nothing :(

    func foo() { print("hello world") } // Swift 2.0 here
}

// Lets say I want to call foo trough the interface
let test = SecondClass()

test.interface.foo() // 'Interface' does not have a member named 'foo'

Is there something I'm doing wrong or do I misunderstand some Swift concepts here?! I do need to subclass here to not to implement everything from super class' protocols over and over again. Is my little pattern even possible? I'd appreciate any help. :)

解决方案

Unfortunately there is no good workaround for this problem.

The main idea to override the typealias would work in this case but consider the following:

protocol TakeAndGet {
    typealias T
    func take(value: T)
    func get() -> T
}

class FirstClass: TakeAndGet {
    typealias T = FirstClass

    var property = 0

    func take(value: T) {
        value.property = 4
    }

    func get() -> T {
        return FirstClass()
    }
}

class SecondClass: FirstClass {

    typealias T = SecondClass

    var property2 = "hello"
}

If the typealias of the SecondClass overrides the other one the take method would work since it takes a subclass which can be treated as the superclass. But the get method cannot implicitly convert FirstClass to SecondClass. Therefore it is not possible to override a typealias.

Now if we want to override the get function with get() -> SecondClass it wouldn't work since it has not the same signature as the one in the superclass. In addition we inherit the get method which results in an ambiguous use:

SecondClass().get() // which type gets returned? SecondClass or FirstClass

So you have to try a different approach.

这篇关于Swift:覆盖子类中的typealias的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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