In java si potrebbe avere un metodo generico simile al seguente, in cui il tipo viene esplicitamente specificato e passato al metodo come argomento. È possibile con swift?Swift - Metodi generici
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);
Quello che sto cercando di realizzare
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? Che ne dici di un commento? – aryaxt
@Zaph L'ho fatto, e so già come scrivere funzioni generiche in modo rapido. Questo è un caso particolare in cui si passa anche il tipo di classe – aryaxt
Sì, i voti bassi di drive-by senza una ragione non sono utili e piuttosto come quelli di una donnola. – zaph