Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created November 5, 2015 16:15
Show Gist options
  • Save odrobnik/54b5f994e94f0f8268f0 to your computer and use it in GitHub Desktop.
Save odrobnik/54b5f994e94f0f8268f0 to your computer and use it in GitHub Desktop.
Why does this work? what is the difference between AnyClass and T.Type? How could this be made more generic?
func classForName(name: String) -> AnyClass
{
// build namespaced name
let namespacedName = "Module." + name
return NSClassFromString(namespacedName)!
}
func nameForType<T where T: Entity>(type: T.Type) -> String
{
return NSStringFromClass(type).stringByReplacingOccurrencesOfString("Module.", withString: "")
}
if let expectedClass = self.classForName(entityName).self as? Entity.Type
{
let result = expectedClass.init(dictionary: ...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment