ios - swift mirror return class name in class function -


i'm having class function , in class function want name of class thats extending abstract entity class.

i found mirror type have this:

class abstractentity {      class func findorcreateentity() -> anyobject? {         print("name: \(nsstringfromclass(mirror(reflecting: self)))")     } } 

the problem example class car extends abstractentity , calls method prints car.type, correct don't want .type extension. there way class name?

i want swift way if it's possible. know of exnteions of nsobject , nsstringfromclass thing..

for clarity:

class abstractentity: nsmanagedobject {      // insert code here add functionality managed object subclass     class func findorcreateentitywithuuid(entityname: string, uuid: string, context: nsmanagedobjectcontext) -> anyobject? {         let request = nsfetchrequest(entityname: entityname)         request.returnsobjectsasfaults = false;          let predicate = nspredicate(format: "uuid = %@", uuid)         request.predicate = predicate          {             var results = try context.executefetchrequest(request)             if results.count > 0 {                 return results[0]             } else {                 let entity =  nsentitydescription.entityforname(entityname, inmanagedobjectcontext: context)                  let abstractentity = nsmanagedobject(entity: entity!, insertintomanagedobjectcontext: context)                 abstractentity.setvalue(uuid, forkey: "uuid")                  return abstractentity             }         } catch let error nserror {             ddlogerror("fetch failed: \(error.localizeddescription)")         }          return nil     } } 

that's abstractentity , every entity in model extends that. have 1 place want logic.

now example have car entity , want car.find... , returns me car. why (think) need class name can use fetch nsfetchrequest(entityname: entityname). pass in entityname want fetch think ugly because when car.find.. know want car.

really don't question, according requirement need following functionality.

class abstractentity {       class func findorcreateentity() {         let str = __file__         let fullnamearr = str.componentsseparatedbystring("/")         print("name: \(fullnamearr.last!)")       } } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -