在java中,您可以使用类似于以下的泛型方法,其中类型被明确指定并作为参数传递给方法。这可能与迅速吗?Swift - 通用方法
public T fetchObject(Class<T> clazz, int id) {
// My table name is the same as class name
// So based on the generic type passed to method I want to fetch that record.
}
User user = fetchObject(User.class, 5);
我试图实现
public func fetchObject (id: Int /* Somehow pass the type */) -> T? {
// I need a way to know what class type has to be used with this generic method
// Create NSEntityDescription based on the generic class passed
// managedObjectContext getExistingObjectById
}
更好-1?如何评论? – aryaxt 2014-08-29 16:21:25
@Zaph我做了,我已经知道如何在swift中编写泛型函数。这是一个特殊的情况,你也通过类型 – aryaxt 2014-08-29 16:32:49
是的,没有理由的驱动器倒票没有帮助,而是黄鼠狼喜欢。 – zaph 2014-08-29 16:36:25