Как привести общий T в соответствие с протоколом

Мне нужно привести общий тип в классе, чтобы соответствовать протоколу. Я не могу использовать ограничения, так как контейнерный класс должен быть сериализован. Итак, как я могу привести в этом случае T к ZNumeric, когда я уже знаю (я могу проверить, как вы видите), что он соответствует протоколу?

//: Playground - noun: a place where people can play

import UIKit

protocol ZNumeric {

}

extension Double: ZNumeric {

}

class GenericClass<T> {

}

class RestrictedGenericClass<T:ZNumeric> {

}

class Container {
    required init?<T>(type: T.Type) {
        let a = GenericClass<T>()
        print(a)

        if T.self is ZNumeric.Type {
            print("is numeric")
            //let b = RestrictedGenericClass<T>() // Will not work obviously
            //print(b)
        }
    }
}

let cDouble = Container(type: Double.self) // if T.self is ZNumeric.Type is true
let cString = Container(type: String.self) // if T.self is ZNumeric.Type is false

2 ответа

Поскольку обобщения должны быть известны во время компиляции, вы не можете просто проверить во время выполнения на соответствие (я почти уверен). Я думаю, что это больше похоже на то, что вы хотите:

class Container {
    required init?<T>(type: T.Type) {
        let a = GenericClass<T>()
        print(a)
    }

    required init?<T : ZNumeric>(type: T.Type) {
        let a = GenericClass<T>()
        print(a)

        print("is numeric")
        let b = RestrictedGenericClass<T>()
        print(b)
    }
}

Более конкретный инициализатор будет выбран во время компиляции.

Я думаю, что вы ищете это.

protocol ZNumeric {

}

extension Double: ZNumeric {

}

class GenericClass<T> {

}

class RestrictedGenericClass<T:ZNumeric> {

}

class Container<T> {
    required init?(value: T) {
    }
    convenience init?(_ value: T) {
        self.init(value: value)
        let a = GenericClass<T>()
        print(a)
    }
}
extension Container where T: ZNumeric {
    convenience init?(_ value: T) {
        self.init(value: value)
        let b = RestrictedGenericClass<T>() // Will not work obviously
        print(b)
    }
}

print("test Double")
let cDouble = Container(1.1) // if T.self is ZNumeric.Type is true
print("test String")
let cString = Container("") // if T.self is ZNumeric.Type is false

// test Double
// RestrictedGenericClass<Swift.Double>
// test String
// GenericClass<Swift.String>
  1. сделать внутренний обязательный инициализатор
  2. сделать инициализатор для T: AnyObject
  3. сделать инициализатор для T: ZNumeric
Другие вопросы по тегам