在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