在返回类

时间:2017-07-13 12:59:51

标签: swift generics swift3 protocols

以下行为应该如此。重要的是,func1func2会返回约束MyClass

MySecondClassProtocol对象的界面
import Foundation

protocol MyIntProtocol: class {

    var value: Int? { get set }
    func fulfill(_ result: Int)

}

final class MyIntClass: MyIntProtocol {

    var value: Int?
    func fulfill(_ result: Int) { self.value = result }

}

protocol MyFirstClassProtocol: class {

    func func1(_ value: MyIntProtocol) -> MySecondClassProtocol

}

protocol MySecondClassProtocol: class {

    func func2(_ value: MyIntProtocol) -> MySecondClassProtocol
    func func3(_ value: MyIntProtocol)

}

final class MyClass: MyFirstClassProtocol, MySecondClassProtocol {

    func func1(_ value: MyIntProtocol) -> MySecondClassProtocol {
        print(value.value!)
        return self
    }

    func func2(_ value: MyIntProtocol) -> MySecondClassProtocol {
        print(value.value!)
        return self
    }

    func func3(_ value: MyIntProtocol) { print(value.value!) }

}

let e = MyIntClass()
e.fulfill(23)
let m = MyClass()
// m has func1, func2 and func3 methods

let n =  m.func1(e)
// n has func2 and func3 methods

let o = n.func2(e)
// o has func2 and func3 methods

o.func3(e)

我想复制这个"返回带有约束的对象到协议"但是,现在引入了泛型类及其协议。

以下编译,但不会将func1func2的返回对象约束到MySecondClassProtocol

import Foundation

protocol MyGenericProtocol: class {

    associatedtype ValueType

    var value: ValueType? { get set }
    func fulfill(_ result: ValueType)

}

final class MyGenericClass<T>: MyGenericProtocol {

    var value: T?

    func fulfill(_ result: T) { self.value = result }

}

protocol MyFirstClassProtocol: class {

    associatedtype T: MyGenericProtocol
    associatedtype U: MySecondClassProtocol

    func func1(_ value: T) -> U

}

protocol MySecondClassProtocol: class {

    associatedtype T: MyGenericProtocol
    associatedtype U: Self

    func func2(_ value: T) -> U
    func func3(_ value: T)

}

final class MyClass: MyFirstClassProtocol, MySecondClassProtocol {

    func func1(_ value: MyGenericClass<Int>) -> MyClass {
        print(value.value!)
        return self
    }

    func func2(_ value: MyGenericClass<Int>) -> MyClass {
        print(value.value!)
        return self
    }

    func func3(_ value: MyGenericClass<Int>) { print(value.value!) }

}

let e = MyGenericClass<Int>()
e.fulfill(23)
let m = MyClass()
// m has func1, func2 and func3 methods

let n = m.func1(e)
// n has func1, func2 and func3 methods
// Wanting only func2 and func3 methods available

let o = n.func2(e)
// o has func1, func2 and func3 methods
// Wanting only func2 and func3 methods available

o.func3(e)

我将如何实现这一目标?谢谢!

1 个答案:

答案 0 :(得分:0)

这似乎是我在寻找的东西。它将返回对象约束为正确的协议。

import Foundation

protocol MyGenericProtocol: class {

    associatedtype ValueType

    var value: ValueType? { get set }
    func fulfill(_ result: ValueType)

}

final class MyGenericClass<T>: MyGenericProtocol {

    var value: T?
    func fulfill(_ result: T) { self.value = result }

}

protocol MyFirstClassProtocol: class {

    func func1<T>(_ value: T) -> MySecondClassProtocol where T: MyGenericProtocol

}

protocol MySecondClassProtocol: class {

    func func2<T>(_ value: T) -> MySecondClassProtocol where T: MyGenericProtocol
    func func3<T>(_ value: T) where T: MyGenericProtocol

}

final class MyClass: MyFirstClassProtocol, MySecondClassProtocol {

    func func1<T>(_ value: T) -> MySecondClassProtocol where T: MyGenericProtocol {
        print(value.value!)
        return self
    }

    func func2<T>(_ value: T) -> MySecondClassProtocol where T: MyGenericProtocol {
        print(value.value!)
        return self
    }

    func func3<T>(_ value: T) where T: MyGenericProtocol {

        print(value.value!)

    }

}

let e = MyGenericClass<Int>()
e.fulfill(23)
let m = MyClass()
// m has func1, func2 and func3 methods

let n =  m.func1(e)
// n has func2 and func3 methods

let o = n.func2(e)
// o has func2 and func3 methods

o.func3(e)