简体   繁体   English

Swift:如何从 function 返回 class 类型

[英]Swift: how to return class type from function

I know it is possible to pass class type to a function in swift:我知道可以将 class 类型传递给 swift 中的 function:

func setGeneric<T>(type: T.Type){ }
setGeneric(Int.self)

But how we can return type from function?但是我们如何从 function 返回类型呢? Writing something like写类似的东西

func getGeneric<T>() -> T.Type {
   return Int.self
}

gives compiler error "Int is not identical to T".给出编译器错误“Int 与 T 不相同”。 So is it possible to return type from a swift function?那么是否可以从 swift function 返回类型?

Edit编辑
Some explanation.一些解释。 I have classes that are used for persistence (I'm using Realm) and I have classes that acts as wrappers around this classes.我有用于持久性的类(我正在使用 Realm),我有一些类充当这些类的包装器。 All wrappers inherits from RealmClassWrapper which needs to know what Realm class it actually wraps.所有包装器都继承自RealmClassWrapper ,它需要知道它实际包装了什么 Realm class。 So lets say I have this realm model:所以假设我有这个 realm model:

class RealmTodo: RLMObject {
   dynamic var title = ""
}

and my wrappers supper class looks like this:我的包装纸晚餐 class 看起来像这样:

class RealmClassWrapper {
   private let backingModel: RLMObject
   //...
   func backingModelType<T>() -> T.Type{ fatalError("must be implemented") }
}

and actual wrapper:和实际包装:

class Todo: RealmClassWrapper {
   //some other properties
   func backingModelType<T>() -> T.Type{ return RealmTodo.self }
}

You can return any type you want. 您可以返回任何您想要的类型。

func getTypeOfInt()  -> Int.Type  { return Int.self  }
func getTypeOfBool() -> Bool.Type { return Bool.self }

If the type is not determined from arguments or if the return is constant, there is no need to introduce a generic T type. 如果类型不是从参数确定的,或者返回是常量,则不需要引入通用T类型。

It works when I modify your function like this: 当我修改你的函数时,它可以工作:

func getGeneric<T>(object: T) -> T.Type {
    return T.self
}

getGeneric(0)    // Swift.Int

You can force the downcast (as!) as below 您可以强制下传(如!),如下所示

func getGeneric<T>() -> T.Type {
  return Int.self as! T.Type
} 

But out of the function scope, you need to indicate the returned type: 但是在函数范围之外,您需要指明返回的类型:

var t:Int.Type = getGeneric()

Yes, this is possible. 是的,这是可能的。 The problem here is that you say your function returns a generic T.type , but you always return Int.type . 这里的问题是你说你的函数返回一个通用的T.type ,但你总是返回Int.type Since T is not always an Int, the compiler raises an error. 由于T不总是Int,编译器会引发错误。

If you don't want to specify the return type you can use AnyClass as it instead of a template parameter.如果你不想指定返回类型,你可以使用AnyClass作为它而不是模板参数。

class A {}

class B {}

public enum ExampleEnum: String {
    case a
    case b
    
    func asClass() -> AnyClass {
        switch self {
        case .a:
            return A.self
        case .b:
            return B.self
        }
    }
}



let myGoal : AnyClass = ExampleEnum.a.asClass()

You can also avoid the final cast to AnyClass, but compiler will show you an error您也可以避免最终转换为 AnyClass,但编译器会向您显示错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM