Swift:如何从函数返回类类型。

时间:2021-10-05 02:05:18

I know it is possible to pass class type to a function in swift:

我知道在swift中传递类类型的函数是可能的:

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

But how we can return type from function? Writing something like

但是如何从函数返回类型呢?写作之类的

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

gives compiler error "Int is not identical to T". So is it possible to return type from a swift function?

给出编译错误“Int不等于T”。那么是否可以从swift函数返回类型呢?

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. All wrappers inherits from RealmClassWrapper which needs to know what Realm class it actually wraps. So lets say I have this realm model:

编辑一些解释。我有用于持久化的类(我使用的是Realm),也有类作为类的包装器。所有的包装器都继承自RealmClassWrapper,它需要知道它实际包装的领域类。假设我有一个领域模型

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

and my wrappers supper class looks like this:

我的包装纸晚餐课是这样的:

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 }
}

4 个解决方案

#1


23  

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类型。

#2


8  

It works when I modify your function like this:

当我这样修改你的函数时,它是有效的:

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

getGeneric(0)    // Swift.Int

#3


2  

You can force the downcast (as!) as below

你可以强制下拉(as!

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()

#4


0  

Yes, this is possible. The problem here is that you say your function returns a generic T.type, but you always return Int.type. Since T is not always an Int, the compiler raises an error.

是的,这是可能的。这里的问题是你说你的函数返回一个通用的T。输入,但是你总是返回Int.type。由于T并不总是整数,编译器会引发错误。

#1


23  

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类型。

#2


8  

It works when I modify your function like this:

当我这样修改你的函数时,它是有效的:

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

getGeneric(0)    // Swift.Int

#3


2  

You can force the downcast (as!) as below

你可以强制下拉(as!

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()

#4


0  

Yes, this is possible. The problem here is that you say your function returns a generic T.type, but you always return Int.type. Since T is not always an Int, the compiler raises an error.

是的,这是可能的。这里的问题是你说你的函数返回一个通用的T。输入,但是你总是返回Int.type。由于T并不总是整数,编译器会引发错误。